简体   繁体   English

“ *”在SQL Server 2016中不起作用

[英]The “*” is not working in SQL Server 2016

I have a SQL query written in SQL Server version 2000. The query is not running in SQL Server 2016. The query is like below. 我有一个用SQL Server 2000版编写的SQL查询。该查询未在SQL Server 2016中运行。查询如下。

Select *
from ProjPace2 P, ProjPace2 P2
where P.DivCode *= P2.DivCode
  and P.ProjGrp *= P2.ProjGrp
  and P.ProjYr *= P2.ProjYr
  and P.T_D *= P2.T_D
  and P.Qtr *= P2.Qtr
  and P.SRA_LRA *= P2.SRA_LRA
  and P.District *= P2.District
  and P.PICompany *= P2.PICompany
  and P.ContCode *= P2.ContCode
  and P.dtWkEnding > dateadd(dd,-1,'1/1/2015')
  and P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')

I am getting the following error: 我收到以下错误:

Msg 4147, Level 15, State 1, Line 20 Msg 4147,第15级,状态1,第20行
The query uses non-ANSI outer join operators ("*=" or "=*"). 该查询使用非ANSI外部联接运算符(“ * =”或“ = *”)。 To run this query without modification, please set the compatibility level for current database to 80, using the SET COMPATIBILITY_LEVEL option of ALTER DATABASE. 要不加修改地运行此查询,请使用ALTER DATABASE的SET COMPATIBILITY_LEVEL选项将当前数据库的兼容性级别设置为80。 It is strongly recommended to rewrite the query using ANSI outer join operators (LEFT OUTER JOIN, RIGHT OUTER JOIN). 强烈建议使用ANSI外部联接运算符(LEFT OUTER JOIN,RIGHT OUTER JOIN)重写查询。 In the future versions of SQL Server, non-ANSI join operators will not be supported even in backward-compatibility modes. 在SQL Server的将来版本中,即使在向后兼容模式下也将不支持非ANSI连接运算符。

I can understand the error is occurring due to "*" and I want to replace it with Left outer join so I can get the same result. 我可以理解由于“ *”而引起的错误,我想将其替换为“左外部联接”,以便获得相同的结果。

Any help will be thankfully accepted. 任何帮助将被感激地接受。

Partha 帕塔

All conditions that are specified with *= operator denote ON clause for LEFT OUTER JOIN. *=运算符指定的所有条件都表示LEFT OUTER JOIN. ON子句LEFT OUTER JOIN. So equivalent query would become: 因此,等效查询将变为:

Select *
from ProjPace2 P
  left outer join ProjPace2 P2 on 
    P.DivCode = P2.DivCode
    and P.ProjGrp = P2.ProjGrp
    and P.ProjYr = P2.ProjYr
    and P.T_D = P2.T_D
    and P.Qtr = P2.Qtr
    and P.SRA_LRA = P2.SRA_LRA
    and P.District = P2.District
    and P.PICompany = P2.PICompany
    and P.ContCode = P2.ContCode
where P.dtWkEnding > dateadd(dd,-1,'1/1/2015')
  and P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')

One note though: Since you have condition where returned rows must have P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015') there is no need for LEFT OUTER JOIN since rows without matching P2 record will never be returned. 但请注意:由于您有条件,返回的行必须P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')具有P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')不需要LEFT OUTER JOIN因为没有匹配P2记录的行将永远不会返回。 So for this query you should use INNER JOIN . 因此,对于此查询,您应该使用INNER JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM