August 12, 2013 at 4:59 am
i have a SP which has a param id as int
and the where claus ein SP had where id=@ID
but now instead of a single vlaue i get id as multiple values
so i need IN operator
redefined ID as varchar
i used where id IN (@ID)
and when executing SP as EXEC usp_itemdeatils '45,72' i get error
Conversion failed when converting the varchar value '45,72' to data type int.
August 12, 2013 at 5:30 am
Hi
It's not working because the WHERE clause becomes:
WHERE id IN ('45,72')
which obviously won't work as you have a varchar value there. One way you could get around it is to use dynamic SQL and add the "IN string" into the query and then execute it.
You may also be able to it by putting the values into a temporary table or table variable and using a join in the query.
Duncan
August 12, 2013 at 6:35 am
Hi,
You can use this solution:
http://mitchelsellers.com/blogs/2008/08/08/using-the-in-clause-with-stored-procedures.aspx
August 12, 2013 at 7:27 am
ohadpick (8/12/2013)
You can use this solution:
You should check out the link that Luis pointed you too, or the one in my signature about splitting strings. It is way faster than a while loop and it is safe from sql injection.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 12, 2013 at 10:32 am
You can also use Table-Valued Parameters
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply