使用“IN”的 SQL 语法问题具有动态数据的关键字
我正在使用 SQL Server 2000,并且在 SQL 代码块方面遇到了一个奇怪的问题(如下):
用户可以输入“GM”作为可能的参数或“F”。如果用户在存储过程查询字符串中输入“GM”作为参数,我需要 AutoDivision 包含 GMC、CAD、CHE、SAT
declare @AutoDivision as varchar(15)
set @AutoDivision = 'GM'
if @AutoDivision = 'GM'
Begin
Select @AutoDivision = '''Cad'', ''GMC'', ''Sat'', ''Che'''
End
SELECT
oh.OrderNumber, lg.[lgh_number]
FROM
[dbo].[OrderHeader] oh (NOLOCK)
INNER JOIN
[dbo].[DistrctHeader] lg (NOLOCK) ON oh.[inv_number] = lg.[inv_number]
INNER JOIN
[dbo].[DealerCompany] c (NOLOCK) ON c.cmp_id = LEFT(oh.[ordernumber],3)
INNER JOIN
[dbo].[divisionXREF] x (NOLOCK) ON x.Division = c.comp_revtype
WHERE
oh.ord_number = '113-889257'
AND x.AutoDivision IN (@AutoDivision)
--AND x.AutoDivision IN ('Cad', 'Sat', 'GMC', 'Che')
AND lg.[lgh_outstatus] IN ('AVAIL', 'PLAN', 'DISP', 'STRTD', 'PEND','COMP')
但是,当我运行下面的代码时,我没有取回任何记录。 当我取消注释代码行时--AND x.AutoDivision IN ('Cad', 'Sat', 'GMC', 'Che')
它有效(我得到一条返回的记录)。
当我执行 print 'AND x.AutoDivision IN (' + cast(@AutoDivision as varchar) + ')'
我返回 AND x.AutoDivision IN ('Cad', 'GMC', 'Sat', 'Che')
我在这里错过了什么?
I am using SQL Server 2000 and having a wierd issue with a SQL code block (below):
A user can enter "GM" as a possible parameter or "F". If the user enters "GM" as a parameter in the stored proc query string, I need the AutoDivision to include GMC, CAD, CHE,SAT
declare @AutoDivision as varchar(15)
set @AutoDivision = 'GM'
if @AutoDivision = 'GM'
Begin
Select @AutoDivision = '''Cad'', ''GMC'', ''Sat'', ''Che'''
End
SELECT
oh.OrderNumber, lg.[lgh_number]
FROM
[dbo].[OrderHeader] oh (NOLOCK)
INNER JOIN
[dbo].[DistrctHeader] lg (NOLOCK) ON oh.[inv_number] = lg.[inv_number]
INNER JOIN
[dbo].[DealerCompany] c (NOLOCK) ON c.cmp_id = LEFT(oh.[ordernumber],3)
INNER JOIN
[dbo].[divisionXREF] x (NOLOCK) ON x.Division = c.comp_revtype
WHERE
oh.ord_number = '113-889257'
AND x.AutoDivision IN (@AutoDivision)
--AND x.AutoDivision IN ('Cad', 'Sat', 'GMC', 'Che')
AND lg.[lgh_outstatus] IN ('AVAIL', 'PLAN', 'DISP', 'STRTD', 'PEND','COMP')
However, when I run the code below, I don't get back any records.
When I uncomment the code line--AND x.AutoDivision IN ('Cad', 'Sat', 'GMC', 'Che')
it works (I get a record returned).
When I do a print 'AND x.AutoDivision IN (' + cast(@AutoDivision as varchar) + ')'
I get back AND x.AutoDivision IN ('Cad', 'GMC', 'Sat', 'Che')
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能使用单个变量来表示 SQL 中以逗号分隔的 IN 参数列表 - Oracle、MySQL、SQL Server...没关系。
要使此变量方法起作用,您需要使用动态 SQL,以便能够首先将查询创建为字符串(使用串联从变量中获取 IN 参数),然后执行查询语句:
我建议阅读在实施动态 SQL 解决方案之前动态 SQL 的诅咒和祝福。
表值函数
表值函数将允许您在不使用动态 SQL 的情况下执行您想要的操作 - 有更多信息请参阅本文。
You can't use a single variable to represent a comma separated list of IN parameters in SQL - Oracle, MySQL, SQL Server... doesn't matter.
To get this variable method to work, you need to use dynamic SQL so you are able to create the query as a string first (using concatenation to get the IN parameters from your variable), and the query statement is executed afterwards:
I recommend reading The Curse and Blessings of Dynamic SQL before implementing a dynamic SQL solution.
Table Valued Function
A table valued function would allow you do what you want without using dynamic SQL -- there's more info in this article.
为了避免这种情况,您可以创建一个临时表,填充它,然后使用
To avoid this, you can create a temp table, fill it in, then use
即使您连接了看起来像是几个不同参数的内容,您的 IN 子句实际上是将其作为单个字符串进行测试(您确实将其声明为 varchar),并且没有记录与该谓词匹配也就不足为奇了。
看起来您正在尝试混合动态 sql 和标准查询。那是行不通的。您的查询要么必须全部动态创建,然后专门执行,要么您的 IN 子句必须输入单独的参数,这可以通过 subqery 完成。
您可以执行以下操作:
或者
试试这个:
这是一个内联子查询,它将任意字符串联合到结果集中。 (持保留态度。我离 Sql Server 界面还很远。)
Even though you concatenate what looks like a few different arguments your IN clause is actually testing it as a single string (you did declare it as a varchar) and it's no surprise that no records match that predicate.
Looks like you're trying to mix dynamic sql and a standard query. That won't work. Your query either has to be all dynamically created and then specially executed or your IN clause has to be inputed with individual arguments which can be done by subqery.
You could do something like:
OR
Try this:
That is an inline subquery that UNIONs arbitrary strings into a result set. (Take with a grain of salt. I am a long way from a Sql Server interface.)
你能澄清一下这两行代码吗,它们不是在做同样的事情吗?
Can you clarify these two lines of code, are they not doing the same thing?