语法Merge Join不正确

时间:2015-10-19 02:56:04

标签: sql sql-server syntax merge syntax-error

相当困惑,SQL Server 2012识别出MERGE子查询的语法问题。但是,当我独自运行子查询时,它可以毫无问题地工作。

已经盯着这一段时间,需要一些新鲜的眼睛。

  

Msg 102,Level 15,State 1,Line 14

     

' CM'

附近的语法不正确      

Msg 102,Level 15,State 1,Line 25

     

CS'附近的语法不正确。

USE [UDARepDBArchive]

INSERT INTO ZZZ_SCD

SELECT [Id]
  ,[ZZZ]
  ,[MMMMM]
  ,[VVVVVVV]                
  ,Eff_Date
  ,End_Date = CONVERT(DATETIME2,END_DATE)
  ,Current_Flag
   FROM

     (MERGE ZZZ_SCD CM
                 USING (SELECT [ID] = GA.ACCOUNTID
                               ,ZZZ = CASE 
                                        WHEN isnull(g.MMMMM,'0') = '0' OR ISNULL(g.VVVVVVV,'0') = '0' 
                                        THEN '0' 
                                        ELSE g.MMMMM /g.VVVVVVV
                                      END 
                               ,MMMMM = G.MMMMM
                               ,VVVVVVV = G.VVVVVVV
                       FROM UDAReporting.rbd.STRGroup G
                       LEFT OUTER JOIN UDAReporting.rbd.STRGroupAccount GA ON GA.GROUPID = G.GROUPID
                       ) CS ON CM.ID = CS.ID

                       WHEN NOT MATCHED THEN
                              INSERT VALUES (cs.[Id]
                                            ,cs.[ZZZ]
                                            ,cs.[MMMMM]
                                            ,cs.[VVVVVVV]                
                                            ,CONVERT(DATETIME2,GETDATE())                                                
                                            ,'2199-10-01 00:00:00.000' 
                                            ,'Y' )
                       WHEN MATCHED AND CM.Current_Flag = 'Y' AND (CM.ZZZ <> CS.ZZZ ) THEN
                              UPDATE SET CM.Current_Flag = 'N', CM.End_date = CONVERT(DATETIME2,GETDATE())
                 OUTPUT $Action Action_Out, cs.[Id]
                                            ,cs.[ZZZ]
                                            ,cs.[MMMMM]
                                            ,cs.[VVVVVVV]                
                                            ,CONVERT(DATETIME2,GETDATE()) AS Eff_date                                               
                                            ,'2199-10-01 00:00:00.000' End_Date
                                            ,'Y' Current_Flag

          ) AS MERGE_OUT
   WHERE MERGE_OUT.Action_Out = 'UPDATE'

1 个答案:

答案 0 :(得分:0)

出现问题是由&#34; SET COMPATIBILITY_LEVEL = 90&#34;在现有的数据库上,我只是在创建一个新的数据库,而不是升级处理潜在的问题