IF Exists(Select * From inserted Where ContactId > 10) BEGIN Insert Into Person.ContactLog ( ContactID,Action ) Select ContactID 'Update' From inserted Where ContactId > 12 END
if (select count(*) from inserted) = 0 then return update MyTableset modifieddate = getdate()inner join inserted onMytable.ID = inserted.ID
ALTER TRIGGER [Person].[uContact] ON [Person].[Contact] AFTER UPDATE NOT FOR REPLICATION AS BEGIN SET NOCOUNT ON; UPDATE [Person].[Contact] SET [Person].[Contact].[ModifiedDate] = GETDATE() FROM #inserted -- non-existent table used intentionally WHERE inserted.[ContactID] = [Person].[Contact].[ContactID];END;
-- row before updateSELECT 'Last Name Before Update' AS TYPE, LastNameFROM person.contactWHERE ContactID = 12GO BEGIN Try BEGIN TRANSACTION UPDATE [AdventureWorks].[Person].[Contact] SET [LastName] = 'Changed' WHERE ContactID = 12 -- this will never be hit due to error in trigger COMMIT TRANSACTION SELECT 'After Commit'END TryBEGIN Catch SELECT 'Error in Trigger' AS error, Error_Message() AS MESSAGE -- if the explicit rollback is not entered another error is raised -- and the transcation is rolled back --RollBack TransactionEND CatchGO-- row not updatedSELECT 'Last Name After Failed Update due to Trigger Error' AS TYPE, LastNameFROM person.contactWHERE ContactID = 12
-- row before updateSELECT 'Last Name Before Update' AS TYPE, LastNameFROM person.contactWHERE ContactID = 12GO BEGIN Try UPDATE [AdventureWorks].[Person].[Contact] SET [LastName] = 'Changed' WHERE ContactID = 12 -- this will never be hit due to error in trigger SELECT 'After Commit'END TryBEGIN Catch SELECT 'Error in Trigger' AS error, Error_Message() AS MESSAGEEND CatchGO-- row not updatedSELECT 'Last Name After Failed Update due to Trigger Error' AS TYPE, LastNameFROM person.contactWHERE ContactID = 12