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
SET @@go = CASE WHEN ... THEN 1 ELSE 0 END
?