I need to create a trigger on a table (after insert) that simply checks for a string in a cell. If the string exists, I need to log an event in the windows event logger. I am not really sure if I am able to use the inserted tables this way, but I thought I would try. This is what I have so far:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER bcpCatchTrigger
ON dbo.TABLE_NAME
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @@Message varchar(255), @@go int;
SET @@go = 0
CASE
WHEN inserted.STAT_MESSAGE LIKE '%bcp.exe%' THEN @@go = 1
END
IF @@go == 1 {
@@Message = inserted.STAT_MESSAGE
EXEC xp_logevent 60000, @@Message, ERROR;
}
END
GO
I get incorrect syntax at the CASE statement but I don't quite see why. Perhaps the issue is actually how I'm accessing the inserted table. Where am I going wrong here?
Thank you.
EDITED CODE:
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER bcpCatchTrigger
ON dbo.TABLE_NAME
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @@Message varchar(255), @@go int;
SET @@go = CASE
WHEN (SELECT STAT_MESSAGE FROM INSERTED) LIKE '%bcp.exe%' THEN 1
ELSE 0
END
IF (@@go = 1) BEGIN
SET @@Message = (SELECT STAT_MESSAGE FROM INSERTED)
EXEC xp_logevent 60000, @@Message, ERROR;
END
END
GO
Best Answer
Let's start with the main issue of this trigger:
This will produce the following error
as soon as your
INSERT
statement will have more than one row.Others are more suggestions than corrections.
insert
did insert any row and if not just exit the trigger (if @@rowcount = 0 return;
)inserted
twice.STAT_MESSAGE LIKE '%bcp.exe%'
(select top 1) but you should think about the case when multiple rowsLIKE '%bcp.exe%'
are inserted: if you want to log all of them you should use a loop.Here is your trigger modified: