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 2000 Forums
 Transact-SQL (2000)
 dynamic in clause

Author  Topic 

jdattis
Starting Member

14 Posts

Posted - 2005-01-25 : 15:33:43
All,

I have a need to pass in multiple string variables into a sp, in the form of:

s1 = '1,2,3'
s2 = '4,5,5,7'

Something like that. I want my query to look like this:

SELECT *
FROM tbl
WHERE Field1 IN (@s1) AND Field2 IN (@s2)

Field1 and Field2 are int's. So, it can't convert. Does anyone have a "better" solution than creating a string and running an exec. That kinda defeats the purpose of the sp in my opinion.

Thanks,

Jake

BlackDog
Starting Member

18 Posts

Posted - 2005-01-25 : 15:35:33
You can create a table function that accepts the string of comma separated values and returns a table with one column and a row for each value. Then you can join with the table function in your select statement.
Go to Top of Page

jdattis
Starting Member

14 Posts

Posted - 2005-01-25 : 15:40:09
quote:
Originally posted by BlackDog

You can create a table function that accepts the string of comma separated values and returns a table with one column and a row for each value. Then you can join with the table function in your select statement.



Hate to sound dumb, but I have never done that before. Do you have the time to type out a simple example?

Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-01-25 : 15:44:24
If you search the forums, you'll find a treasure trove of stuff

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830



Brett

8-)
Go to Top of Page

jdattis
Starting Member

14 Posts

Posted - 2005-01-25 : 16:16:02
What would be the best way to handle an empty string situation. For example, say the source web page had checkboxes, and none were checked.
Go to Top of Page

BlackDog
Starting Member

18 Posts

Posted - 2005-01-25 : 17:27:42
Sorry for the lack of detail in my posting, I was in a hurry. The link brett posted shows a good example.

Using the table function, it will handle the empty string situation when you join to the table function. If you have 10 checkboxes and they don't select any, then when you pass the string into the table function you will end up with a table that has either no rows or 10 rows with values of NULL (depending on how you want to handle it.) So when you join tbl to the table function, no rows will match on the join and no data will be returned.

HTH

Ryan
Go to Top of Page
   

- Advertisement -