旧版本= *(右连接)不接受新版本

时间:2016-03-11 09:39:54

标签: sql sql-server tsql join outer-join

以下是旧查询,该查询在当前版本中无效。

select b.location,
       case when cmailid is null then cmail else cmailid end as cname,
       b.role,
       a.cmailid,  
       CONVERT(VARCHAR(10), a.date_cert, 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), a.date_cert, 22), 11)) as certdate,
       c.order1,  
       cast(exception as varchar(100)) as exception, 
       isnull(convert(varchar(50),a.date_cert,113),'') as statusForLink, 
       '' as statusForGrid 
from tbl_sc_details a, 
     tbl_sc_admin b, 
     tbl_sc_roleorder c 
where a.location=*b.location
  and b.role=c.role 
  and b.role='LOG' 
  and quart='4th QUARTER' 
  and year='2016'

希望这是由于加入符号(= *),PLease帮助获得替代

3 个答案:

答案 0 :(得分:0)

将旧语法更改为ANSI语法

 SELECT b.location,
       CASE
           WHEN cmailid IS NULL THEN cmail
           ELSE cmailid
       END AS cname,
       b.role,
       a.cmailid,
       CONVERT(VARCHAR(10), a.date_cert, 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), a.date_cert, 22), 11)) AS certdate,
       c.order1,
       cast(exception AS varchar(100)) AS exception,
       isnull(convert(varchar(50),a.date_cert,113),'') AS statusForLink,
       '' AS statusForGrid
FROM tbl_sc_details a
RIGHT OUTER JOIN tbl_sc_admin b 
    ON a.location=b.location
INNER JOIN tbl_sc_roleorder c 
    ON b.role=c.role
WHERE b.role='LOG'
  AND quart = '4th QUARTER'
  AND YEAR='2016'

答案 1 :(得分:0)

您需要使用正确的JOINS。 = *和* =是非标准JOIN。

= *代表右外连接,* =代表左外连接。

您可以更好地使用显式JOINS加入您的表格。

请参阅MSDN

  

使用非标准产品特定语法指定外部联接   和WHERE子句。 =运算符用于指定左外部   join和= 运算符用于指定右外连接。

您可以尝试这样:

SELECT b.location,
       CASE
           WHEN cmailid IS NULL THEN cmail
           ELSE cmailid
       END AS cname,
       b.role,
       a.cmailid,
       CONVERT(VARCHAR(10), a.date_cert, 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), a.date_cert, 22), 11)) AS certdate,
       c.order1,
       cast(exception AS varchar(100)) AS exception,
       isnull(convert(varchar(50),a.date_cert,113),'') AS statusForLink,
       '' AS statusForGrid
FROM tbl_sc_details a
RIGHT JOIN tbl_sc_admin b 
    ON a.location=b.location
INNER JOIN tbl_sc_roleorder c 
    ON b.role=c.role
WHERE b.role='LOG'
  AND quart = '4th QUARTER'
  AND YEAR='2016'

答案 2 :(得分:0)

ANSI的时间。

SELECT b.location, 
       CASE WHEN cmailid IS NULL 
            THEN cmail 
            ELSE cmailid 
       END AS cname,
       b.role, 
       a.cmailid,  
       CONVERT(VARCHAR(10), a.date_cert, 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), a.date_cert, 22), 11)) AS certdate,
       c.order1,  
       CAST(exception AS varchar(100)) AS exception, 
       ISNULL(CONVERT(varchar(50),a.date_cert,113),'') AS statusForLink, 
       '' AS statusForGrid 
FROM tbl_sc_details a
RIGHT JOIN tbl_sc_admin b ON a.location = b.location
INNER JOIN tbl_sc_roleorder c ON b.role = c.role 
WHERE b.role = 'LOG' 
AND quart = '4th QUARTER' 
AND year = '2016'

如果您仍然使用ANSI兼容查询获取错误数据,则需要重新考虑您的连接/参数。