Passing parameters to IN clause in SQL Server

San picture San · Jan 18, 2011 · Viewed 21.4k times · Source

Possible Duplicates:
Parameterizing a SQL IN clause?
SQL Server - In clause with a declared variable

Hi,

I am facing problem passing parameters to 'IN' clause. I am using the below query.

Query:

SELECT Topics.Topic_Id  FROM Topics 
    Where Topic_Description IN (''+ @Topics +'')    

This query works when the parameter has single value. My parameter can have comma separated multiple values like : 'one','two','three','four'. But the query fails whenever there are multiple parameters. How to get over this ? Please suggest.

Thanks

I have to use the above as part of a stored procedure. I have to take the result of the select query into a cursor like below:

DECLARE cur_TopicIDs CURSOR FOR SELECT Topics.Topic_Id FROM Topics Where Topic_Description IN (''+ @Topics +'')....etc

In this case how can I use dynamic sp as suggested in other links

Answer

Georgi picture Georgi · Jan 18, 2011

Use any of the split functions from here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648