将参数传递给 SQL Server 中的 IN 子句
你好,
我我面临将参数传递给“IN”子句的问题。我正在使用以下查询。
查询:
SELECT Topics.Topic_Id FROM Topics
Where Topic_Description IN (''+ @Topics +'')
当参数具有单个值时,此查询有效。我的参数可以有逗号分隔的多个值,例如:“一”、“二”、“三”、“四”。但只要有多个参数,查询就会失败。如何克服这个问题?请建议。
谢谢,
我必须使用上述内容作为存储过程的一部分。我必须将选择查询的结果放入游标中,如下所示:
DECLARE cur_TopicIDs CURSOR FOR 从主题中选择 Topics.Topic_Id Topic_Description IN (''+ @Topics +'')....等
在这种情况下,我如何按照其他链接中的建议使用动态 sp
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此处的任何 split 函数:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
Use any of the split functions from here:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
您必须创建一个
one','two','third
格式的字符串作为 @Topics 中的值。编辑:-
然后你需要使用
EXEC sp_executesql
来执行你的sql语句。 spender 针对您的问题给出的链接中正确解释了答案。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.