t-sql语句语法错误

时间:2011-11-04 02:11:03

标签: sql-server tsql syntax-error

需要t-sql语句语法错误的帮助。完整陈述如下。 在sql-server 2005上使用sqlcmd。

-- oitm to oitg is a 1 to 1 link via the 64 matching columns
select g.[ItmsGrpNam], b.[ItmsGrpNam], count(1) 
from      oitg g
     join (select distinct 
           i.ItmsGrpCod as [ItmsGrpCod],
           i.QryGroup1 as [QryGroup1], 
           i.QryGroup2 as [QryGroup2] 
           from oitm i) p  -- pseudo intersect table for oitg-oitm links
     join oitb b  on b.[ItmsGrpCod] = p.[ItmsGrpCod]
where (g.ItmsGrpNam in  -- translate interect to oitg names
       (select x.ItmsGrpNam from oitg x where x.ItmsTypCod=1 and p.[QryGroup1]='Y') 
      )
group by g.ItmsGrpNam, b.ItmsGrpNam

到目前为止的结果。

select g.[ItmsGrpNam], b.[ItmsGrpNam], count(1) 
from      oitg g
     join (select distinct 
           i.ItmsGrpCod as [ItmsGrpCod],
           i.QryGroup1 as [QryGroup1], 
           i.QryGroup2 as [QryGroup2] 
           from oitm i) p  -- pseudo intersect table for oitg-oitm links
     join oitb b  on b.[ItmsGrpCod] = p.[ItmsGrpCod]

此部分语句给出以下错误。 消息102,级别15,状态1,服务器,第10行 'ItmsGrpCod'附近的语法不正确。

上述语句中的选择将按原样返回。

3> select distinct
4>            i.ItmsGrpCod as [ItmsGrpCod],
5>            i.QryGroup1 as [QryGroup1],
6>            i.QryGroup2 as [QryGroup2]
7>            from oitm i
8> go
ItmsGrpCod QryGroup1 QryGroup2
---------- --------- ---------
       100 N         N
       101 N         Y
       102 N         Y
       103 N         Y
       104 N         Y
       105 N         Y
       106 N         Y
       107 N         N
       108 N         N
       108 Y         N
       110 N         Y
       111 N         N
       112 N         Y
       113 N         N

尝试用“内部联接”替换“加入”。已开始用方括号括起列名。 ItmsGrpCod是oitm和oitb之间的fk,名称相同。 (在SAP土地上并非总是如此!)

对于那些感兴趣的人,这是我尝试编写围绕SAP B1设计缺陷的oitm(订购商品) to oitg(item properties)no intersect table hell。

2 个答案:

答案 0 :(得分:0)

您可以发布这些表的架构吗? oitb有一个ItmsGrpCod列吗? SQL Server因错误报告错误而臭名昭着,但请注意,在查询中使用ItmsGrpCod四次。

答案 1 :(得分:0)

您错过了第一次加入的on声明。也许是这样的:

-- oitm to oitg is a 1 to 1 link via the 64 matching columns
select g.[ItmsGrpNam], b.[ItmsGrpNam], count(1) 
from      oitg g
     join (select distinct 
           i.ItmsGrpCod as [ItmsGrpCod],
           i.QryGroup1 as [QryGroup1], 
           i.QryGroup2 as [QryGroup2] 
           from oitm i) p  -- pseudo intersect table for oitg-oitm links
       on g.ItmsGrpCod = p.ItmsGrpCod
     join oitb b  on b.[ItmsGrpCod] = p.[ItmsGrpCod]
where (g.ItmsGrpNam in  -- translate interect to oitg names
       (select x.ItmsGrpNam from oitg x where x.ItmsTypCod=1 and p.[QryGroup1]='Y') 
      )
group by g.ItmsGrpNam, b.ItmsGrpNam
相关问题