此TSQL中逗号附近的语法无效在哪里? [英] Where is the Invalid syntax near the comma in this TSQL?

查看:110
本文介绍了此TSQL中逗号附近的语法无效在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有从查询中获得结果。起初我认为这只是一个糟糕的查询 - 不应该返回任何数据*,但现在看来它可能是导致问题的语法错误。



在Visual Studio的服务器资源管理器中运行存储过程(查询是其中的一部分)时,我从第一个查询中获取数据,但是这个(更新)不返回任何数据(没有错误消息,但没有数据,或)。然而,当我尝试在LINQPad中运行存储过程的这一部分时,各地编码器的同伴声称它已经发现了令人恐惧的*错误102:错误的语法附近','。*



它指向这一行:



 项目代码 选择 ItemCode 来自 UnitProducts 其中单位= '  BIG RED ONE')),'  X'





...(唯一带','的行),但我不知道问题是什么。我还尝试了这个主题的变化:



  ItemCode < span class =code-keyword> in (选择 ItemCode 来自 UnitProducts 其中单位= '  BIG RED ONE'),< span class =code-string>'  X'))





...但错误的消息是一样的。



这是我在LINQPad中尝试的整个查询:



 isnull((选择  top   1  ItemCode 
From PlatypusUnitMapping
其中单位= ' BIG RED ONE'
MemberNo = ' 42'
MemberItemCode = ' 314IMPie'
ItemCode in 选择 ItemCode 来自 UnitProducts 其中​​单位= ' BIG RED ONE')),' X'
其中 NHItemCode = ' X'





那么这有什么问题?更简单的查询版本有效:



 选择  top   1  ItemCode 
来自 PlatypusUnitMapping
其中单位= ' BIG RED ONE'
MemberNo = ' 42'
MemberItemCode = ' 314IMPie'
ItemCode in
选择 ItemCode 来自 UnitProducts 其中单位= ' BIG RED ON E'





...但我需要isNull爵士乐来进行后续查询。什么语法无效,如何使其有效/修复?

解决方案

引发错误是因为条件中的括号不匹配所以从语法点来看查看 IN 子句中的逗号超出了 IN 子句。



而不是这个

  中的ItemCode 选择 ItemCode 来自 UnitProducts < span class =code-keyword>其中单位= '  BIG RED ONE') ),'  X'



cosider这个

  ItemCode  in (( 选择 ItemCode 来自 UnitProducts 其中单位= < span class =code-string>'  BIG RED ONE'),' < span class =code-string> X')





但是,这不是最大的问题。当您使用 IN 子句时,您必须决定是使用子查询获取值还是提供预定义的文字列表。你不能混用这些。



一种简单的方法是修改子查询并使用 UNION 来获取字面值与查询值相结合。例如,请考虑以下内容

  SELECT  * 
FROM sysobjects
WHERE id IN SELECT id
FROM sysobjects
WHERE id > 0
UNION
SELECT -1072372588
UNION
SELECT -1064594347)


I'm not getting results back from a query. At first I thought it was just a bad query - that no data *should* be returned, but now it seems it could be a syntax error that's causing the problem.

When running the Stored Procedure (that the query is a part of) in Visual Studio's Server Explorer, I get data back from the first query, but this one (an update) doesn't return any data (no err msg, but no data, either). Yet, when I try to run just this portion of the Stored Procedure in LINQPad, that bosom companion of coders everywhere declaims that it has discovered the much-dreaded "*Error 102: Incorrect syntax near ','.*"

It points to this line:

and ItemCode in (Select ItemCode from UnitProducts where Unit='BIG RED ONE')),'X')



...(the only line with a ','), but I don't see what the problem is. I also tried this variation on the theme:

and ItemCode in (Select ItemCode from UnitProducts where Unit='BIG RED ONE'),'X'))



...but the err msg is the same.

Here is the entire query that I'm trying in LINQPad:

isnull((Select top 1 ItemCode
From PlatypusUnitMapping
where Unit='BIG RED ONE'
and MemberNo='42'
and MemberItemCode= '314IMPie'
and ItemCode in (Select ItemCode from UnitProducts where Unit='BIG RED ONE')),'X')
Where NHItemCode='X'



So what is wrong with this? The simpler version of the query works:

Select top 1 ItemCode 
    From PlatypusUnitMapping 
    where Unit='BIG RED ONE' 
    and MemberNo='42' 
    and MemberItemCode= '314IMPie' 
    and ItemCode in 
    (Select ItemCode from UnitProducts where Unit='BIG RED ONE')



...but I need the "isNull" jazz for a subsequent query. What syntax is invalid, and how can I make it valid/fix it?

解决方案

The error is raised because you have mismatched parenthesis in the condition so from syntactical point of view the comma in the IN clause is outside the IN clause.

Instead of this

and ItemCode in (Select ItemCode from UnitProducts where Unit='BIG RED ONE')),'X')


cosider this

and ItemCode in ((Select ItemCode from UnitProducts where Unit='BIG RED ONE'),'X')



However, this is not the biggest problem. When you use IN clause you have to decide whether you fetch the values using a subquery or if youprovide a predefined literal list. You can't mix these.

One simple way is to modify the subquery and use UNION to fetch the literal values combined to the queried values. For example, consider the following

SELECT *  
FROM sysobjects 
WHERE id IN (SELECT id 
             FROM sysobjects 
             WHERE id > 0
             UNION
             SELECT -1072372588
             UNION
             SELECT -1064594347)


这篇关于此TSQL中逗号附近的语法无效在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆