Inline SQL Throwing Error

I keep getting this error when running the below syntax.  What causes this, and how can I resolve it?

Msg 512, Level 16, State 1, Procedure _do this, Line 114
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.



SET @Query = 	'update ##Results '
	+ 'set Count14 = '
	+ '(select count(*) from '+@fulllocation+' '
	+ 'where abcd in (select '
	+ 'namica_abcdfrom '
	+ 'masterlocale.'
	+ 'dbo.main '
	+ 'where cfl_id in '
	+ '(''' +@store+ ''')) '
	+ 'and saledate between ''' + convert(varchar(100),@fromDate,101) + ''' '
	+ 'and ''' + convert(varchar(100),@toDate,101) + ''' '
	+ 'and supervisorapproved is null) '
	+ 'where storeName = ''' + @store + ''' '
exec (@Query)
	
set @query = 'update ##Results '
	+ 'set Count0 = '
	+ '(select count(*) from '+@fulllocation+' '
	+ 'where abcd in (select '
	+ 'namica_abcd from '
	+ 'masterlocale.'
	+ 'dbo.main '
	+ 'where frankflie_id in '
	+ '(''' +@store+ ''')) '
	+ 'and saledate between ''' + convert(varchar(100),@fromDate,101) + ''' '
	+ 'and ''' + convert(varchar(100),@toDate,101) + ''') '
	+ 'where storeName = ''' + @store+ ''' '					
exec (@Query)			
	
set @query = 'update ##Results '
	+ 'set Count6 = '
	+ '(select count(*) from '+@fulllocation+' '
	+ 'where abcd in (select '
	+ 'namica_abcd from '
	+ 'masterlocale.'
	+ 'dbo.main '
	+ 'where needmoreinfo is not null '
	+ 'and stepford_id in '
	+ '(''' +@store+  ''')) '
	+ 'and saledate between ''' + convert(varchar(100),@fromDate,101) + ''' '
	+ 'and ''' + convert(varchar(100),@toDate,101) + ''') '
	+ 'where storeName = ''' + @store+ ''' '
exec (@Query)

set @query = 'update ##Results '
	+ 'set count3 = '
	+ '(select count(*) from '+@fulllocation+' '
	+ 'where abcd in (select '
	+ 'namica_abcdfrom '
	+ 'masterlocale.'
	+ 'dbo.main '
	+ 'where prgl_prg_id in '
	+ '(''' + @store+ ''')) '
	+ 'and shipped_date is not null '
	+ 'and shipped_Date between ''' + convert(varchar(100),@fromDate,101) + ''' '
	+ 'and ''' + convert(varchar(100),@toDate,101) + ''') '
	+ 'where storeName = ''' +@store+ ''' '
exec (@Query)

August 25th, 2015 3:40pm

What you have written so far makes very little sense. Why do you need to do your updates dynamically? I suggest to start from writing a working static query with parameters. 

Why do you need to use global temp table? Can you explain the problem you're trying to solve with this weird code and provide DDL of your tables, some insert statements with fake data and desired result?

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:50pm

I'll work on getting DDL & Garbage data provided.  Essentially what I am trying to do is get the desired counts (from the inline sql) and input them into a Global Temp Table.  But the statements throw the error, and I have never seen that b4 so I am not sure what to do to resolve. 
August 25th, 2015 3:54pm

Well, when you're writing dynamic query the first step to troubleshoot is to use print @Query instead of exec(@Query). Then all errors will be easier to spot.

Also, it is a very bad practice to embed parameters into the SQL. Much better practice is to keep parameters being parameters and use sp_executeSQL stored procedure with parameters.

Finally, you need to have a very good reason for using dynamic SQL. If the problem can be solved without resorting to dynamic SQL, in 99% of cases it's better to keep the statement static.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:59pm

Dynamic SQL is being used as fully qualified database names are being stored in the dynamic sql.  So the variable @fulllocation contains a fully qualified database that is being queried.  I could not think of a way of doing such w/o dynamic SQL
August 25th, 2015 4:01pm

That may be a valid reason for using dynamic SQL (although the reasons leading to storing database name in dynamically may be questioned).

Anyway, first you need to start from the static query and get the static query working. Once you get the static query working correctly, you can convert it to dynamic. That's the normal practice on writing dynamic query - has a clear understanding of the query itself and have it tested with a few variations of possible @fulllocation (for start, using it with normal va

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 4:16pm

The queries work before converting them to inline.  Their is something going haywire with this particular dataset that is being queried (and I'm sure once I am able to provide DDL this will become apparent to someone here)
August 25th, 2015 4:31pm

Do you have the query working correctly with parameters? What do you have as a query when you print it and try to execute?
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 4:51pm

Subquery returned more than 1value.This is notpermitted whenthe subquery follows =, !=, <, <= , >, >= or whenthe subquery isused asan expression.

Your subquery returns more than a single value... You can't make many values equal to a single one.

There's little point in guessing at where

August 25th, 2015 5:20pm

Add a param so you can change whether to PRINT or EXEC your SQL. Find one parm setting that work and one that gives this error. Then run both of these with the print option set to 1. Now you can study the SQL that work and the SQL that doesn't work. If you don't find something obvious, then check for triggers on the tables that you modify, then error will likely be from bad trigger code.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 5:21pm

Dynamic SQL is being used as fully qualified database names are being stored in the dynamic sql.  So the variable @fulllocation contains a fully qualified database that is being queried.  I could not think of a way of doing such w/o dynamic SQL

You could use synonyms. You would need to use dynamic SQL to defined synonym:

SELECT @sql = 'CREATE SYNONYM sometbl FOR ' + @fullocation'
EXEC(@sql)

But this permits you keep the UPDATE statements clean - and within your frame of competence.

August 25th, 2015 5:42pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics