2017-09-29 72 views
0
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

CREATE TRIGGER [dbo].[ProviderOffice_UPDATE] 
ON [dbo].[ProviderOffice] 
AFTER UPDATE 
AS 
BEGIN 
    BEGIN TRY; 
    BEGIN TRANSACTION; 
     SET QUOTED_IDENTIFIER ON; 
     SET ANSI_PADDING ON; 

     UPDATE ProviderOffice 
     SET ProviderOffice.LastUpdateID = USER_NAME(), 
      ProviderOffice.LastUpdateDateTime = GETDATE()   
     FROM ProviderOffice 
     JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
         AND ProviderOffice.LocationNbr = inserted.Provider_HEUID 
     IF EXISTS (SELECT Provider_HEUID, LocationNBR 
        FROM INSERTED 
        WHERE EligibilityFlag = 1 
        AND INSERTED.OfficeTypeCode1 IN (2, 4, 6, 8, 10, 12, 14, 16) 
        AND INSERTED.OfficeTypeCode2 IN (2, 4, 6, 8, 10, 12, 14, 16) 
        AND INSERTED.OfficeTypeCode3 IN (2, 4, 6, 8, 10, 12, 14, 16)) 
      UPDATE ProviderOffice 
      SET EligibilityFlag = 2 
      FROM ProviderOffice 
      JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
          AND ProviderOffice.LocationNbr = inserted.LocationNbr 
      WHERE (inserted.EligibilityFlag = 1 
        AND INSERTED.OfficeTypeCode1 in (2, 4, 6, 8, 10, 12, 14, 16) 
        AND INSERTED.OfficeTypeCode2 in (2, 4, 6, 8, 10, 12, 14, 16) 
        AND INSERTED.OfficeTypeCode3 in (2, 4, 6, 8, 10, 12, 14, 16)) 
     ELSE 
      IF EXISTS (SELECT Provider_HEUID, LocationNBR 
         FROM INSERTED 
         WHERE INSERTED.OfficeTypeCode1 = 1 OR 
           INSERTED.OfficeTypeCode1= 3 OR 
           INSERTED.OfficeTypeCode1= 5 OR 
           INSERTED.OfficeTypeCode1= 7 OR 
           INSERTED.OfficeTypeCode1= 9 OR 
           INSERTED.OfficeTypeCode1= 11 OR 
           INSERTED.OfficeTypeCode1= 13 OR 
           INSERTED.OfficeTypeCode1= 15) 
       UPDATE ProviderOffice 
       SET EligibilityFlag = 1 
       FROM ProviderOffice 
       JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
           AND ProviderOffice.LocationNbr = inserted.LocationNbr 
       WHERE INSERTED.OfficeTypeCode1 = 1 OR 
         INSERTED.OfficeTypeCode1 = 3 OR 
         INSERTED.OfficeTypeCode1 = 5 OR 
         INSERTED.OfficeTypeCode1 = 7 OR 
         INSERTED.OfficeTypeCode1 = 9 OR 
         INSERTED.OfficeTypeCode1 = 11 OR 
         INSERTED.OfficeTypeCode1 = 13 OR 
         INSERTED.OfficeTypeCode1 = 15 
     ELSE 
      IF EXISTS (SELECT Provider_HEUID, LocationNBR 
         FROM INSERTED 
         WHERE INSERTED.OfficeTypeCode2 = 1 OR 
          INSERTED.OfficeTypeCode2 = 3 OR 
          INSERTED.OfficeTypeCode2 = 5 OR 
          INSERTED.OfficeTypeCode2 = 7 OR 
          INSERTED.OfficeTypeCode2 = 9 OR 
          INSERTED.OfficeTypeCode2 = 11 OR 
          INSERTED.OfficeTypeCode2 = 13 OR 
          INSERTED.OfficeTypeCode2 = 15) 
       UPDATE ProviderOffice 
       SET EligibilityFlag = 1 
       FROM ProviderOffice 
       JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
          AND ProviderOffice.LocationNbr = inserted.LocationNbr 
       WHERE INSERTED.OfficeTypeCode2 = 1 
        OR INSERTED.OfficeTypeCode2 = 3 
        OR INSERTED.OfficeTypeCode2 = 5 
        OR INSERTED.OfficeTypeCode2 = 7 
        OR INSERTED.OfficeTypeCode2 = 9 
        OR INSERTED.OfficeTypeCode2 = 11 
        OR INSERTED.OfficeTypeCode2 = 13 
        OR INSERTED.OfficeTypeCode2 = 15 
     ELSE 
      IF EXISTS (SELECT Provider_HEUID, LocationNBR 
         FROM INSERTED 
         WHERE INSERTED.OfficeTypeCode3 = 1 OR 
          INSERTED.OfficeTypeCode3 = 3 OR 
          INSERTED.OfficeTypeCode3 = 5 OR 
          INSERTED.OfficeTypeCode3 = 7 OR 
          INSERTED.OfficeTypeCode3 = 9 OR 
          INSERTED.OfficeTypeCode3 = 11 OR 
          INSERTED.OfficeTypeCode3 = 13 OR 
          INSERTED.OfficeTypeCode3 = 15) 
       UPDATE ProviderOffice 
       SET EligibilityFlag = 1 
       FROM ProviderOffice 
       JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
          AND ProviderOffice.LocationNbr = inserted.LocationNbr 
       WHERE INSERTED.OfficeTypeCode3 = 1 OR 
        INSERTED.OfficeTypeCode3 = 3 OR 
        INSERTED.OfficeTypeCode3 = 5 OR 
        INSERTED.OfficeTypeCode3 = 7 OR 
        INSERTED.OfficeTypeCode3 = 9 OR 
        INSERTED.OfficeTypeCode3 = 11 OR 
        INSERTED.OfficeTypeCode3 = 13 OR 
        INSERTED.OfficeTypeCode3 = 15 
    ELSE 
     IF EXISTS (SELECT Provider_HEUID, LocationNBR 
        FROM INSERTED 
        WHERE INSERTED.OfficeTypeCode1 = 0 AND 
         INSERTED.OfficeTypeCode2 = 0 AND 
         INSERTED.OfficeTypeCode3 = 0) 
      UPDATE ProviderOffice 
      SET EligibilityFlag = 3 
      FROM ProviderOffice 
      JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
         AND ProviderOffice.LocationNbr = inserted.LocationNbr 
      WHERE INSERTED.OfficeTypeCode1 = 0 AND 
       INSERTED.OfficeTypeCode2 = 0 AND 
       INSERTED.OfficeTypeCode3 = 0 

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

這個腳本是關於「觸發不發射」的寶貴問題。有什麼想法嗎?該腳本是DML觸發器,旨在更新表格,但觸發器未觸發。有什麼想法嗎?

+0

你怎麼知道這不是射擊?它和try/catch塊有什麼不同?任何時候在觸發器中發生錯誤都會回滾事務。在觸發器中進行顯式事務並不是一個好主意,因爲如果您調用了正在使用事務的代碼,則trancount將會關閉並導致很多其他問題。 –

回答

0

的代碼塊是最有可能的問題:

UPDATE ProviderOffice 
    SET ProviderOffice.LastUpdateID = USER_NAME(), 
     ProviderOffice.LastUpdateDateTime = GETDATE()   
    FROM ProviderOffice 
    JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
        AND ProviderOffice.LocationNbr = inserted.Provider_HEUID 

其他地方一樣,在JOIN上插入是

JOIN inserted ON ProviderOffice.Provider_HEUID = inserted.Provider_HEUID 
       AND ProviderOffice.LocationNbr = inserted.LocationNbr 

是有一些原因,不是大腦抽筋等,第一查詢試圖加入ProviderOffice.LocationNbrinserted.Provider_HEUID

換句話說,大多數觸發器可能正在工作,但是您對LastUpdateId的更新和LastUpdateDateTime不正確。

NOTE:這是更新上次更新和上次更新應該在代碼中的主要原因,或者是與上述條件更新分開的觸發器。如果上述至少一個條件應該爲真,則應該在每個條件塊中完成對用戶和更新時間的更新。是的,代碼是重複的,但是你有條件更新+用戶/時間更新同時運行。