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