问题
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
回答1:
Use any of the split functions from here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
回答2:
You have to create a string of format one','two','three
as value in the @Topics.
EDIT:-
Then you need to use EXEC sp_executesql
to execute your sql statement. The answer is properly explained in the link given by spender for your question.
来源:https://stackoverflow.com/questions/4723100/passing-parameters-to-in-clause-in-sql-server