在sql server中获取关键字附近的错误语法错误

时间:2018-02-02 13:56:57

标签: sql sql-server

我正在将mysql查询转换为sql查询我有以下查询,当我在sql server中运行此查询时,它说

Error 156: Incorrect syntax near the keyword 'as'.

任何人都可以帮忙,为什么我会收到此错误?


Mysql Query(这在mysql中运行正常):

UPDATE  tb_Episode as e left JOIN tb_Payer as p ON (e.CustID = p.company_id) AND (e.PayorType = p.payor_type)
                        left JOIN tb_HHPPS as h ON e.HHPPS = h.HHPPS
                        SET
                                e.PayerType = If(p.payer_type=1,"Ep","NonEp"),

                                e.LUPAAlert = If((p.payer_type)<>"1",0,If(EpEnd<=Now(),0,If(TotVisits<5,1,0))),
                                e.LUPADays = If((If((p.payer_type)<>"1",0,If(EpEnd<=Now(),0,If(TotVisits<5,1,0))))=0,0,EpEnd-Now()),
                                e.FinalAlert = If((p.payer_type)<>"1",0,If(abs(DATEDIFF(Now(),EpEnd))>1,0,1)),
                                e.FinalDays = If((If((p.payer_type)<>"1",0,If(abs(DATEDIFF(Now(),EpEnd))>1,0,1)))=1,abs(DATEDIFF(Now(),EpEnd)),0),
                                e.RAPAlert = If(p.payer_type="1",If(abs(DATEDIFF(Now(),EpStart))>0,1,0),0),
                                e.RAPDays = If((If(p.payer_type="1",If(abs(DATEDIFF(Now(),EpStart))>0,1,0),0))=1,abs(DATEDIFF(Now(),EpStart)),0)

                        where  e.billed_flag = "0"


SQLQuery(获取错误):

UPDATE  tb_Episode as e left JOIN tb_Payer as p ON (e.CustID = p.company_id) AND (e.PayorType = p.payor_type)
                        left JOIN tb_HHPPS as h ON e.HHPPS = h.HHPPS
                        SET
                                e.PayerType = IIF(p.payer_type=1,'Ep','NonEp'),

                                e.LUPAAlert = IIF((p.payer_type)<>"1",0,IIF(EpEnd<=getdate(),0,IIF(TotVisits<5,1,0))),
                                e.LUPADays = IIF((IIF((p.payer_type)<>"1",0,IIF(EpEnd<=getdate(),0,IIF(TotVisits<5,1,0))))=0,0,EpEnd-getdate()),
                                e.FinalAlert = IIF((p.payer_type)<>"1",0,IIF(abs(DATEDIFF(millisecond,getdate(),EpEnd))>1,0,1)),
                                e.FinalDays = IIF((IIF((p.payer_type)<>"1",0,IIF(abs(DATEDIFF(millisecond,getdate(),EpEnd))>1,0,1)))=1,abs(DATEDIFF(millisecond,getdate(),EpEnd)),0),
                                e.RAPAlert = IIF(p.payer_type="1",IIF(abs(DATEDIFF(millisecond,getdate(),EpStart))>0,1,0),0),
                                e.RAPDays = IIF((IIF(p.payer_type="1",IIF(abs(DATEDIFF(millisecond,getdate(),EpStart))>0,1,0),0))=1,abs(DATEDIFF(millisecond,getdate(),EpStart)),0)

                        where  e.billed_flag = '0'

2 个答案:

答案 0 :(得分:3)

这应该有效。 SQL Server不使用e的语法,它仍然需要UPDATE [TABLE] AS [Alias]子句。

FROM

答案 1 :(得分:1)

如果你像这样移动JOIN语句,它可能会有效:

UPDATE [e]
    SET 
        [e].[PayerType] = IIF([p].[payer_type] = 1, 'Ep', 'NonEp')
      , [e].[LUPAAlert] = IIF(([p].[payer_type]) <> "1", 0, IIF([EpEnd] <= GETDATE(), 0, IIF([TotVisits] < 5, 1, 0)))
      , [e].[LUPADays] = IIF((IIF(([p].[payer_type]) <> "1", 0, IIF([EpEnd] <= GETDATE(), 0, IIF([TotVisits] < 5, 1, 0)))) = 0, 0, [EpEnd] - GETDATE())
      , [e].[FinalAlert] = IIF(([p].[payer_type]) <> "1", 0, IIF(ABS(DATEDIFF([millisecond], GETDATE(), [EpEnd])) > 1, 0, 1))
      , [e].[FinalDays] = IIF((IIF(([p].[payer_type]) <> "1", 0, IIF(ABS(DATEDIFF([millisecond], GETDATE(), [EpEnd])) > 1, 0, 1))) = 1, ABS(DATEDIFF([millisecond], GETDATE(), [EpEnd])), 0)
      , [e].[RAPAlert] = IIF([p].[payer_type] = "1", IIF(ABS(DATEDIFF([millisecond], GETDATE(), [EpStart])) > 0, 1, 0), 0)
      , [e].[RAPDays] = IIF((IIF([p].[payer_type] = "1", IIF(ABS(DATEDIFF([millisecond], GETDATE(), [EpStart])) > 0, 1, 0), 0)) = 1, ABS(DATEDIFF([millisecond], GETDATE(), [EpStart])), 0)
FROM [tb_Episode] AS [e]
LEFT JOIN [tb_Payer] AS [p]
     ON([e].[CustID] = [p].[company_id])
       AND ([e].[PayorType] = [p].[payor_type])
LEFT JOIN [tb_HHPPS] AS [h]
     ON [e].[HHPPS] = [h].[HHPPS]
WHERE 
    [e].[billed_flag] = '0';

但是,由于缺少表定义和语句的复杂性,我无法测试此语句。