2017-09-10 35 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