?
Solved

Subquery returned more than 1 value.

Posted on 2006-03-28
6
Medium Priority
?
478 Views
Last Modified: 2012-06-21
Here's another Where Clause question.

AND a.AccountID IN (COALESCE(@AccountID, (SELECT Items FROM dbo.SplitIDs(@AccountIDs)), a.AccountID))

This line does not work because you can't use a SELECT query in an expression that returns more than one value. The problem is that a single ID integer value may be passed in, or a string of several comma separated IDs may be passed (which is split by a UDF that returns a table of integers), or possibly no ID is passed in at all, in which case we only want to search on other possible parameters and not have to worry about the ID field at all. Can this be done in a single procedure or do I just need to write two procedures, one for passing in integers and one for passing in strings? (One procedure would just make for easier maintenance.)

Thanks

John
0
Comment
Question by:danburyit
  • 3
  • 2
6 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 750 total points
ID: 16311547
I suspect the problem is that COALESCE can't handle a table result in its input.

Remove the COALESCE and make sure another way that NULLs do not get included:


AND (@AccountIDs IS NULL OR a.AccountID IN (SELECT Items FROM dbo.SplitIDs(@AccountIDs) WHERE Items IS NOT NULL))
0
 
LVL 11

Expert Comment

by:Ved Prakash Agrawal
ID: 16311732
Hi Coalesce is used to give the value which get first not null.
and it can accept only comma seprated values But your Select Queries return without comma seprated values.


AND a.AccountID IN (COALESCE(@AccountID, (SELECT top 1 Items FROM dbo.SplitIDs(@AccountIDs)), a.AccountID))

if you can use this then it wil work.
0
 
LVL 1

Author Comment

by:danburyit
ID: 16312246
I am aware that COALESCE will not work. Our friendly SQL Parser doesn't like subqueries that return more than one value used as an expression. Using only the top 1 is not an acceptable solution. What I have in my query is the possibility that a single ID will be passed to the query in the @AccountID parameter, or that several IDs will be passed in the @AccountIDs parameter, or that no value will be passed for either of those two parameters. While we all can agree that the COALESCE example I included in my original post will not work, it does illustrate the logic of what I need to accomplish: AccountID = @AccountID OR AccountID IN @AccountIDs OR don't filter anything on the AccountID field. Hope this clarifies.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16312329
Yes, and I believe the version I posted does that.
0
 
LVL 1

Author Comment

by:danburyit
ID: 16313061
AND (@AccountIDs IS NULL OR a.AccountID IN (SELECT Items FROM dbo.SplitIDs(@AccountIDs) WHERE Items IS NOT NULL))

This phrase works only using the @AccountIDs variable, but neglects to consider the @AccountID variable that passes in a single integer. But that's actually OK because even if I'm only passing in a single account ID, I can still use the @AccountIDs string variable and do away with the @AccountID integer variable altogether. The extra overhead of calling the SplitIDs function on a string with just one account id is pretty minimal. So in the end, I can pass in one ID, a comma separated list of IDs or no ID at all and the query works as it should. Kudos for Mr. Pletcher.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16313569
Sorry, you're right, I actually didn't even notice that there were separate variables for single and multiple ids :blush: .
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question