SQL Server - 带有声明变量的 In 子句
假设我得到以下信息:
DECLARE @ExcludedList VARCHAR(MAX)
SET @ExcludedList = 3 + ', ' + 4 + ' ,' + '22'
SELECT * FROM A WHERE Id NOT IN (@ExcludedList)
错误:将 varchar 值“,”转换为数据类型 int 时转换失败。
我明白为什么会出现错误,但我不知道如何解决它......
Let say I got the following :
DECLARE @ExcludedList VARCHAR(MAX)
SET @ExcludedList = 3 + ', ' + 4 + ' ,' + '22'
SELECT * FROM A WHERE Id NOT IN (@ExcludedList)
Error : Conversion failed when converting the varchar value ', ' to data type int.
I understand why the error is there but I don't know how to solve it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将 @your_list、Your_table 和值替换为真实内容。
上面的 select 语句将执行与以下相同的操作:
Replace @your_list, Your_table and the values with real stuff.
The select statement abowe will do the same as:
您需要将其作为动态 sp 执行,例如
You need to execute this as a dynamic sp like
我知道我正在回复一篇旧帖子,但我想分享一个当人们想避免使用动态 SQL 时如何使用变量表的示例。我不确定这是否是最有效的方法,但是过去当动态 SQL 不是一个选项时,这对我有用。
I know I'm responding to an old post but I wanted to share an example of how to use Variable Tables when one wants to avoid using dynamic SQL. I'm not sure if its the most efficient way, however this has worked in the past for me when dynamic SQL was not an option.
您不能在
IN
子句中使用变量 - 您需要使用 动态 SQL ,或使用函数(TSQL 或 CLR)将值列表转换为一个表。动态SQL示例:
You can't use a variable in an
IN
clause - you need to use dynamic SQL, or use a function (TSQL or CLR) to convert the list of values into a table.Dynamic SQL example:
首先,创建一个快速函数,将分隔的值列表拆分到一个表中,如下所示:
然后像这样调用该函数...
这在我们的项目中效果非常好...
当然,也可以执行相反的操作,如果是这样的话(虽然不是你的问题)。
在处理具有可选多选参数列表的报表时,这确实非常有用。如果该参数为 NULL,您希望选择所有值,但如果它有一个或多个值,您希望根据这些值过滤报告数据。然后像这样使用 SQL:
这样,如果 @ExcludeList 是 NULL 值,则连接中的 OR 子句将成为关闭对此值的过滤的开关。非常方便...
First, create a quick function that will split a delimited list of values into a table, like this:
Then call the function like this...
This has worked really well on our project...
Of course, the opposite could also be done, if that was the case (though not your question).
And this really comes in handy when dealing with reports that have an optional multi-select parameter list. If the parameter is NULL you want all values selected, but if it has one or more values you want the report data filtered on those values. Then use SQL like this:
This way, if @ExcludeList is a NULL value, the OR clause in the join becomes a switch that turns off filtering on this value. Very handy...
我认为问题在于
将其更改为
SET @ExcludedListe ,这样您的查询应该变成
要么
或
I think problem is in
change it to
SET @ExcludedListe such that your query should become
either
or
试试这个:
然后这样称呼它:
Try this:
And then call it like this:
我有另一种解决方案,无需动态查询即可完成此操作。
我们也可以借助 xquery 来完成此操作。
这是完整的解决方案:
http://raresql.com/2011/12/21/how-to-use-multiple-values-for-in-clause-using-same-parameter-sql-server/
I have another solution to do it without dynamic query.
We can do it with the help of xquery as well.
Here is the complete solution :
http://raresql.com/2011/12/21/how-to-use-multiple-values-for-in-clause-using-same-parameter-sql-server/