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;
这个脚本是关于“触发不触发”的宝贵问题。有什么想法吗?
答案 0 :(得分: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.LocationNbr
加入inserted.Provider_HEUID
换句话说,大多数触发器可能都有效,但是对LastUpdateId和LastUpdateDateTime的更新没有正确进行。
注意:这是上次更新和上次更新的更新应该在代码中的主要原因,或者是与上述条件更新的更新相同的主要原因。如果上述条件中的至少一个条件为真,则应在每个条件块中完成对用户和更新时间的更新。是的,代码会重复,但您可以同时运行条件更新+用户/时间更新。