SQL查询的语法不正确

时间:2017-07-04 10:40:00

标签: sql sql-server sql-server-2012

所以我有这个SQL Server查询:

declare @symbol varchar(8) = '180460';
declare @sdate DATE = '20170630';
declare @edate DATE = '20170704';
IF @symbol = 'TSE00081'
    BEGIN
        SELECT * from dbo.IndexNames
    END
ELSE
BEGIN
    SELECT t1.qcode, t4.Name,t1.effectivedate, t1.publishdate, EventMap.Description, t1.SharesChanged, t1.FFW, t1.OldFFW,
    IIF(t1.EventCode='SK',t6.IndexName,''), IIF(t1.EventCode='SK',t7.Indexname,''), IIF(t1.Market<>'',t5.IndexName,''), IIF(t3.NameEn<>'',t3.NameEn,t2.NameEn), 
    IIF(t1.OldSector<>'',t8.NameEn,''),  IIF(t1.Allotment1<>0 AND t1.EventCode<>'05', t1.Allotment2/t1.Allotment1,''), t1.SharesUnit
    FROM ( dbo.AdvNoticeData as t1 , dbo.IdTable as t4 )
    LEFT JOIN dbo.EventMap on t1.EventCode = dbo.EventMap.EventCode
    LEFT JOIN dbo.Industries as t2 on t1.SectorAsOf = t2.IndNum
    LEFT JOIN dbo.Industries as t3 on t1.Sector = t3.IndNum
    LEFT JOIN dbo.Industries as t8 on t1.OldSector = t8.IndNum
    LEFT JOIN dbo.IndexNames as t5 on t1.OldMarket = t5.Market
    LEFT JOIN dbo.IndexNames as t6 on t1.Scale = t6.Scale
    LEFT JOIN dbo.IndexNames as t7 on t1.OldScale = t7.Scale 
    WHERE (t1.Market=1 or t1.MarketAsOf=1 or (t1.OldMarket=1 and t1.MarketAsOf=''))
    and t1.EffectiveDate >= @sdate and t1.EffectiveDate <= @edate and t1.Qcode = t4.Qcode 
    ORDER BY t1.EffectiveDate, t1.EventCode, t1.Qcode
END

问题出在FROM子句之后。我继续在&#39;,&#39;

当我在sqlyog上尝试此命令(几乎没有修改)时,它在FROM子句之后使用该逗号。什么似乎是问题?这是有效的mysql计数器部分:

SELECT t1.qcode, t4.Name,t1.effectivedate, t1.publishdate, 
EventMap.Description, t1.SharesChanged, t1.FFW, t1.OldFFW, 
IF(t1.EventCode="SK",t6.IndexName,""),
IF(t1.EventCode="SK",t7.Indexname,""),
IF(t1.market<>"",t5.IndexName,""),
IF(t3.NameEn<>"",t3.NameEn,t2.NameEn),
IF(t1.oldSector<>"",t8.NameEn,""),
IF(t1.allotment1<>0 AND t1.eventcode<>"05",t1.allotment2/t1.allotment1,""),
t1.sharesunit
FROM (AdvNoticeData AS t1, IdTable AS t4)
LEFT JOIN EventMap ON t1.eventcode = EventMap.eventcode
LEFT JOIN Industries AS t2 ON t1.sectorasof = t2.IndNum
LEFT JOIN Industries AS t3 ON t1.sector = t3.IndNum
LEFT JOIN Industries AS t8 ON t1.oldsector = t8.IndNum
LEFT JOIN IndexNames AS t5 ON t1.oldmarket = t5.Market
LEFT JOIN IndexNames AS t6 ON t1.scale = t6.Scale
LEFT JOIN IndexNames AS t7 ON t1.oldscale = t7.Scale
WHERE (t1.market=1 OR t1.marketasof=1 OR (t1.oldmarket=1 AND t1.marketasof=""))
AND t1.effectivedate >= '20170630'
AND t1.effectivedate <= '20170703'
AND t1.Qcode = t4.Qcode
ORDER BY t1.effectivedate, t1.eventcode,t1.qcode

2 个答案:

答案 0 :(得分:3)

不要混合传统和新的连接语法。

...
FROM dbo.AdvNoticeData as t1
JOIN dbo.IdTable as t4 ON t1.Qcode = t4.Qcode
LEFT JOIN dbo.EventMap on t1.EventCode = dbo.EventMap.EventCode
...

答案 1 :(得分:1)

使用INNER JOIN

修改旧的JOIN语法
declare @symbol varchar(8) = '180460';
declare @sdate DATE = '20170630';
declare @edate DATE = '20170704';
IF @symbol = 'TSE00081'
    BEGIN
        SELECT * from dbo.IndexNames
    END
ELSE
BEGIN
    SELECT t1.qcode, t4.Name,t1.effectivedate, t1.publishdate, EventMap.Description, t1.SharesChanged, t1.FFW, t1.OldFFW,
    IIF(t1.EventCode='SK',t6.IndexName,''), IIF(t1.EventCode='SK',t7.Indexname,''), IIF(t1.Market<>'',t5.IndexName,''), IIF(t3.NameEn<>'',t3.NameEn,t2.NameEn), 
    IIF(t1.OldSector<>'',t8.NameEn,''),  IIF(t1.Allotment1<>0 AND t1.EventCode<>'05', t1.Allotment2/t1.Allotment1,''), t1.SharesUnit
    FROM dbo.AdvNoticeData as t1
    INNER JOIN dbo.IdTable as t4 ON t1.Qcode = t4.Qcode
    LEFT JOIN dbo.EventMap on t1.EventCode = dbo.EventMap.EventCode
    LEFT JOIN dbo.Industries as t2 on t1.SectorAsOf = t2.IndNum
    LEFT JOIN dbo.Industries as t3 on t1.Sector = t3.IndNum
    LEFT JOIN dbo.Industries as t8 on t1.OldSector = t8.IndNum
    LEFT JOIN dbo.IndexNames as t5 on t1.OldMarket = t5.Market
    LEFT JOIN dbo.IndexNames as t6 on t1.Scale = t6.Scale
    LEFT JOIN dbo.IndexNames as t7 on t1.OldScale = t7.Scale 
    WHERE (t1.Market=1 or t1.MarketAsOf=1 or (t1.OldMarket=1 and t1.MarketAsOf=''))
    and t1.EffectiveDate >= @sdate and t1.EffectiveDate <= @edate 
    ORDER BY t1.EffectiveDate, t1.EventCode, t1.Qcode
END