I'm trying to create a trigger in SQL Server 2008 R2 for auditing purposes. To get the proper time zone for different locations across the country I am trying to pass in a variable to an IF/ELSE IF statement to change the hours using the DATEADD() function. Here is my code:
CREATE TRIGGER epic_cover_insert
ON epic_cover
AFTER INSERT
AS
BEGIN
DECLARE @facilityCode INT
SET @facilityCode = (SELECT RIGHT(order_num, 3) FROM epic_cover)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
UPDATE epic_cover
IF @facilityCode = 403 --for logan
BEGIN
SET Created_By = CASE WHEN Created_By IS NULL THEN SUSER_NAME()
WHEN APP_NAME() = 'Microsoft SQL Server Management Studio - Query' THEN SUSER_NAME()
ELSE Created_By END,
Created_On = DATEADD(HH,-1,GETDATE()
END
ELSE IF @facilityCode = 203 -- for Thermont
BEGIN
SET Created_By = CASE WHEN Created_By IS NULL THEN SUSER_NAME()
WHEN APP_NAME() = 'Microsoft SQL Server Management Studio - Query' THEN SUSER_NAME()
ELSE Created_By END,
Created_On = DATEADD(HH,1,GETDATE())
END
ELSE IF @facilityCode = 263 --for Saint Charles
BEGIN
SET Created_By = CASE WHEN Created_By IS NULL THEN SUSER_NAME()
WHEN APP_NAME() = 'Microsoft SQL Server Management Studio - Query' THEN SUSER_NAME()
ELSE Created_By END,
Created_On = GETDATE()
END
END
END
I'm getting all sorts of errors that all say EXPECTING CONVERSATION. I'm not sure what this means. I googled it and that just added to the confusion.
First when writing a trigger you must think about more than 1 record being inserted, updated, or deleted at a time!
I am assuming sql server looking at your code, but if a different rdbms please note it.
Next
SET @facilityCode = (SELECT RIGHT(order_num, 3) FROM epic_cover)
Is always going to give you the facility code that is already in epic_cover which is not what you want.
Then you are trying to mix and IF statement with an update statement which is not allowed. But the reality is you don't any of the if and you can just use a case statement.
Then you are updating all of the records in the table rather than just the ones that were inserted. You will need a primary key on the table if you don't have one or a way of figuring out which records where added! Then make use of the special "inserted"
table.
CREATE TRIGGER epic_cover_insert
ON epic_cover
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
UPDATE epic_cover
SET Created_By = CASE WHEN i.Created_By IS NULL THEN SUSER_NAME()
WHEN APP_NAME() = 'Microsoft SQL Server Management Studio - Query' THEN SUSER_NAME()
ELSE i.Created_By END
,Created_On = CASE
-- right function is for strings so I assume order num is a string if not you will need to do some casting
WHEN RIGHT(i.order_num, 3) = '403' THEN DATEADD(HH,-1,GETDATE())
WHEN RIGHT(i.order_num, 3) = '203' THEN DATEADD(HH,1,GETDATE())
WHEN RIGHT(i.order_num, 3) = '263' THEN GETDATE()
ELSE NULL-- ?????
END
FROM
epic_cover e
INNER JOIN inserted i
ON e.PrimaryKey = i.PrimaryKey
END