Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Passing multiple values to a sp

Author  Topic 

fezz
Starting Member

12 Posts

Posted - 2011-01-20 : 10:43:51
I want to be able to pass the values to my stored procedure which as part of the query looks for them using an IN statement which is fine but i cant seem to pass multiple values to the sp in the first place.

values = 1,2,3

exec some_sp_name @val = '1' works but only for 1 item.

Whats the best way of doing this?
Thanks



Pete

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-01-20 : 10:46:05
Pass them as a csv string.
Then in the SP either use dynamic sql or (preferably) parse the csv string into a table and use that (I would use a cte to do it).

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-01-20 : 12:55:39
The best/preferred method is the use a Table Valued Parameter.
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-01-20 : 14:08:09
quote:
Whats the best way of doing this?




The worst, most non-relational way to do this is tp write a parser in T-SQL. The most proprietary way is a table type declaration. For the painful details of this read:

http://www.simple-talk.com/sql/learn-sql-server/values()-and-long-parameter-lists/

http://www.simple-talk.com/sql/learn-sql-server/values()-and-long-parameter-lists---part-ii/


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -