I have been using a TSQL trigger under the assumption that the inserted table always contains records for an insert or update and the deleted table always contains records for updates (I am ignoring deletes).
Here is the related MSDN article:
http://msdn.microsoft.com/en-us/library/ms191300.aspx
However, I have encountered situations where both inserted and deleted are empty. Here is a test trigger I have been using.
CREATE TRIGGER [dbo].[InsertUpdateTest] ON [dbo].[Test]
AFTER INSERT, UPDATE
AS
DECLARE @countInserted INT
DECLARE @countDelete INT
SET @countInsert = (SELECT COUNT(*) FROM INSERTED)
SET @countDeleted = (SELECT COUNT(*) FROM DELETED)
IF (@countInserted = 0 AND @countDelete = 0)
BEGIN
print 'Inserted and deleted are both empty'
END
Under what conditions does this occur?
Answering my own question. This occurs when an update statement on the triggered table doesn’t update any rows. The trigger still fires but with empty inserted and deleted tables. For example: