Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

This is my query :

SQL
DECLARE @execquery AS NVARCHAR(MAX)

DECLARE @SELECT VARCHAR(MAX) = (SELECT   UserCode + '_EmailAddressInfo' FROM SUser_Master)



SET @execquery =

'SELECT

'+ @SELECT + '.EmailId

FROM '+ @SELECT + '

GROUP BY '+ @SELECT + '.EmailId

'

EXEC(@execquery )



But Error Is Occured

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 15-Nov-14 1:34am
v2
Comments
OriginalGriff 15-Nov-14 7:34am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

The error is pretty self explanatory. Your SELECT query returns more than one value - and you can't assign more than one value to a single NVARCHAR.
Try this:
SQL
DECLARE @SELECT VARCHAR(MAX) = (SELECT   UserCode + '_EmailAddressInfo' FROM SUser_Master WHERE myColumn='Some Value that only returns a single row')
 
Share this answer
 
Comments
Ankit Patel(AP) 15-Nov-14 7:42am    
BUT THIS Query Retun More then One Value
OriginalGriff 15-Nov-14 8:03am    
Yes - that is what the error is saying!
And you can't return more than one value and use it in these circumstances.

So why are you trying to retrieve all the email addresses in the table and try to build a an sql command from it?
Hello Ankit,

SQL
DECLARE @SELECT VARCHAR(MAX) = (SELECT   UserCode + '_EmailAddressInfo' FROM SUser_Master)


Here you are trying to store result of the Query to a variable @SELECT.
But what if this Query returns more than one value? SQL got confused that which value it should store?
that's why error says "SubQuery Returns More Than One Value."

Here You Have some options...
Either you put specific where condition that returns exact one value...
Or Take top 1 like "Select top 1 userCode from SUser_Master".(Last choice for me).
Or Concat the Result (using ',' or '|' or etc.) and store that into your variable.

Hope You have understand the issue and this might help you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900