SQL Server 2005.
I have a stored procedure which accepts a few parameters that may or may not actually have data passed to them. So they may come through as an empty string, or they may come through with data.
The data would be a delimited list of IDs… manufacturer ID, catalog ID, etc. The proc searches through records, and if any of those parameters have data, it’s used in the where clause.
FYI: Split is a function that splits a string (accepts a delimiter) and returns a table.
WHERE
...
AND m.Mfg_ID in (CASE WHEN @manufacturerIds <> '' THEN (SELECT * FROM Split(@manufacturerIds, '|')) ELSE (select m.Mfg_Id) END)
I get an error of:
“Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.”
What I don’t understand is that if I remove the CASE statement, so that I just have this…
WHERE
...
AND m.Mfg_ID in (SELECT * FROM Split(@manufacturerIds, '|'))
… it runs OK; however, this keeps me from being able to pass an empty string.
Any help would be much appreciated!
The difference is that
(select m.Mfg_Id)returns a single row, but(select * from split(...))returns multiple rows. Acasereturns a single value, so it doesn’t know what to do with the multiple rows fromSplit.Instead of a
caseyou could union them together:The bottom half of the
uniononly runs when@manufacturerIds = ''.