Passing parameters to IN clause in SQL Server [duplicate]

自作多情 提交于 2020-04-13 06:14:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!