2017-09-10 37 views
1

我有用SQL Server 2000版编写的SQL查询。查询未在SQL Server 2016中运行。查询如下所示。“*”在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') 

我收到以下错误:

Msg 4147, Level 15, State 1, Line 20
The query uses non-ANSI outer join operators ("*=" or "=*"). 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. It is strongly recommended to rewrite the query using ANSI outer join operators (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.

我能理解错误是由于发生的“*”和我想与左外取代它加入这样我就可以得到相同的结果。

任何帮助将被感谢地接受。这与*=运营商指定的

帕塔

+0

当你写...这个'ProjPace2 P加入ProjPace2 P2在p1.somecol = p2.somecol'而不是'ProjPace2 P,ProjPace2 P2'上时发生了什么? – TheGameiswar

+0

这个代码中'*'的含义是P. DivCode * = P2.DivCode' – TheGameiswar

+1

1.将ProjPace2 P,ProjPace2 P2'从ProjPace2 P左外连接ProjPace2 P2'改为'(可能是对的,不确定)。 2.将'where'改为'on' 3.删除'*'符号。 4.将倒数第二个'和'更改为'where' –

回答

7

所有条件表示为LEFT OUTER JOIN.所以等效查询ON子句将变成:

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') 

一个注意,虽然:既然你有状态,其中返回的行必须有P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')有不需要LEFT OUTER JOIN,因为不会匹配P2记录的行将不会返回。所以对于这个查询,你应该使用INNER JOIN