在我的触发器代码中出现语法错误

时间:2017-09-26 14:41:31

标签: sql

USE DATAJASS_PROCESS_DATABASE;
GO
CREATE TRIGGER [dbo].[Tr_ProviderInduction_Insert] 
   ON [dbo].[ProviderInduction]
   AFTER Insert
   AS
      BEGIN
      BEGIN TRY;
      BEGIN TRANSACTION;
      SET QUOTED_IDENTIFIER ON;
      SET ANSI_PADDING ON;


    UPDATE  ProviderInduction
        set ProviderInduction.CreatedByID = USER_NAME(),
            ProviderInduction.CreatedDateTime = getdate(),
            ProviderInduction.LastUpdateID = USER_NAME(),
            ProviderInduction.LastUpdateDateTime = getdate()            
       FROM (ProviderInduction
            JOIN INSERTED
            on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID)
 CASE WHEN EXISTS(SELECT Provider_HEUID FROM INSERTED
             WHERE INSERTED.EligibilityFlag = 1
             AND INSERTED.SpecialtyCode1 in ('AR', 'ACA', 'PTH', 'ATP', 'AN', 'BBK', 'CTR', 'PCH', 'CIP', 'CLP', 'CCA', 
             'CCE', 'FLX', 'PCP', 'DMP', 'DR', 'EMS', 'FOP', 'HMP', 'HPA', 'HPD', 'HOS','IMA', 'INM', 'MM', 'MGG', 'MGP', 
             'MSR', 'NP', 'RNR', 'NM', 'NR', 'OAN', 'APM', 'PAN', 'PP', 'PDN', 'PDR', 'RO', 'RP', 'R', 'SP', 'SMA', 'VIR','ACC')
            UPDATE ProviderInduction
            SET EligibilityFlag = 2
            FROM ProviderInduction
            JOIN INSERTED
            on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
            WHERE INSERTED.EligibilityFlag = 1
            AND INSERTED.SpecialtyCode1 
            in ('AR', 'ACA', 'PTH', 'ATP', 'AN', 'BBK', 'CTR', 'PCH', 'CIP', 'CLP', 'CCA', 
                'CCE', 'FLX', 'PCP', 'DMP', 'DR', 'EMS', 'FOP', 'HMP', 'HPA', 'HPD', 'HOS','IMA', 'INM', 'MM', 'MGG', 'MGP', 
                'MSR', 'NP', 'RNR', 'NM', 'NR', 'OAN', 'APM', 'PAN', 'PP', 'PDN', 'PDR', 'RO', 'RP', 'R', 'SP', 'SMA', 'VIR','ACC')

 ELSE UPDATE ProviderInduction
       SET EligibilityFlag = 1
       FROM ProviderInduction
       JOIN INSERTED
       on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
       WHERE ProviderInduction.Provider_HEUID= INSERTED.Provider_HEUID


     WHEN INSERTED.AmbCareCode = 3 OR INSERTED.AmbCareCode = 4
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode = 3 OR INSERTED.AmbCareCode = 4

     WHEN INSERTED.AmbCareCode = ''
       THEN UPDATE ProviderInduction
        SET EligibilityFlag =3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode = ''

     WHEN INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = 2
       THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = 2

     WHEN INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = ''
       THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = ''

     WHEN INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = 1 AND INSERTED.FedFacilityFlag = ''
       THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = 1 AND INSERTED.FedFacilityFlag = ''

    WHEN INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = 1 AND INSERTED.FedFacilityFlag = 2 AND INSERTED.PtntsSampleWeekFlag = ''
       THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode = 2 AND INSERTED.AmbCareFlag = 1 AND INSERTED.FedFacilityFlag = 2 AND INSERTED.PtntsSampleWeekFlag = '' 

    WHEN INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= 2
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= 2

    WHEN INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag = ''
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag = ''

    WHEN INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag = 2
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag = 2

    WHEN INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag=2 AND INSERTED.PtntsSampleWeekFlag= ''
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag=2 AND INSERTED.PtntsSampleWeekFlag= ''

    WHEN INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag=2 AND INSERTED.PtntsSampleWeekFlag= 2
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 2 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag=2 AND INSERTED.PtntsSampleWeekFlag= 2

    WHEN INSERTED.AmbCareCode= 1 AND  INSERTED.FedFacilityFlag= ''
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND  INSERTED.FedFacilityFlag= ''

    WHEN INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag= ''
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag= ''

    WHEN INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag= 2
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag= 2

    WHEN INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag=1 AND INSERTED.PtntsSampleWeekFlag= ''
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 1 AND INSERTED.PrivatePtntsFlag=1 AND INSERTED.PtntsSampleWeekFlag= ''

    WHEN INSERTED.AmbCareCode= 1 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= 2
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND INSERTED.AmbCareFlag= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= 2

    WHEN INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= ''
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 3
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= ''

    WHEN INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= 2
        THEN UPDATE ProviderInduction
        SET EligibilityFlag = 2
        FROM ProviderInduction
        JOIN INSERTED
        on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
        WHERE INSERTED.AmbCareCode= 1 AND INSERTED.FedFacilityFlag= 2 AND INSERTED.PtntsSampleWeekFlag= 2

     ELSE UPDATE ProviderInduction
       SET EligibilityFlag = 1
       FROM ProviderInduction
       JOIN INSERTED
       on ProviderInduction.Provider_HEUID = INSERTED.Provider_HEUID
       WHERE ProviderInduction.Provider_HEUID= INSERTED.Provider_HEUID
END CASE)


COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH;
END;

我正在尝试执行此触发器但是我遇到了语法错误。使用逻辑,我希望触发器在插入SpecialtyCode1中的任何指定代码时将eligibilityFlag设置为2。如果该条件为真,那么触发器应该在那里停止。如果没有,触发器应该进入下一个条件,并根据条件继续运行。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您无法使用您正在尝试的语法,可以将其压缩为此伪代码:

UPDATE MyTable
SET SomeColumns=SomeValues
WHERE SomeConditions

CASE WHEN EXISTS(SomeQuery)
UPDATE MyTable ...

CASE表达式不能用于控制UPDATE是否发生。它只能在查询中返回一个值。

你可以做的是使用IF语句来控制UPDATE,但就个人而言,我只是让每个UPDATE都运行,并使用WHERE子句对其进行过滤,以便只有所需的行受到影响(如果存在的话)。如果没有行传递WHERE子句条件,则不会发生UPDATE。

UPDATE MyTable
SET MyColumn=1
WHERE ConditionsForValue1AreTrue

UPDATE MyTable
SET MyColumn=2
WHERE ConditionsForValue2AreTrue

etc.

最后,正如其他人在评论中指出的那样,您需要仔细检查括号的使用,以消除由这些引起的语法错误。