views:

1653

answers:

3

I have two triggers After Insert or Update and Instead of Insert. It appears that the after trigger is not running or sending the correct data.

I have verified the correct operation of Z_UpdateStageTable stored procedure and the Instead of Insert trigger. Removing the Instead of Insert trigger doesn't have any affect. The After Insert, Update trigger was working correctly at one time, I haven't made any changes to it. I have tried deleting it and adding it, but it still doesn't run or have the correct data.

Any Ideas?

Instead of Insert:

ALTER TRIGGER [DeleteExistingFilter]
   ON  [dbo].[Z_MobileSyncFilters]
INSTEAD OF INSERT
AS 
BEGIN
SET NOCOUNT ON;
DELETE FROM Z_MobileSyncFilters WHERE UserID = (SELECT UserID FROM INSERTED);
INSERT INTO Z_MobileSyncFilters
SELECT *
FROM INSERTED;
END

After Insert, Update:

TRIGGER [UpdateStageTable] 
   ON  [dbo].[Z_MobileSyncFilters]
   AFTER INSERT,UPDATE
AS 
BEGIN
SET NOCOUNT ON;
DECLARE @AllWos AS VARCHAR(5000);
DECLARE @PmWos AS VARCHAR(5000);
DECLARE @RepWos AS VARCHAR(5000);
SET @AllWos = (SELECT AllWos FROM INSERTED);
SET @RepWos = (SELECT AllWos FROM INSERTED);
SET @PmWos = (SELECT AllWos FROM INSERTED);
EXEC Z_UpdateStageTable @AllWos;
EXEC Z_UpdateStageTable @RepWos;
EXEC Z_UpdateStageTable @PmWos;
END
+1  A: 

Hold on a second, if userid is your PK then Z_MobileSyncFilters will not have data yet, this is also an instead of trigger

this wholw block doesn't do anything really, why do you need this trigger?

DELETE FROM Z_MobileSyncFilters WHERE UserID = (SELECT UserID FROM INSERTED);
INSERT INTO Z_MobileSyncFilters
SELECT *
FROM INSERTED;

you second trigger is flawed because it will faile if you have a multi row operation

why do you have 2 insert trigger (1 instead 1 after) on this table?

SQLMenace
The app (not mine) has new data every time it connects. Unfortunately the app is not smart enough to do an update so it tries to do an insert very time it connects. Without the delete it only works the first time.
NitroxDM
Dennis is right the second trigger is incorrect and will not work correctly if multiple row inserts happen. Never rely on only one row being inserted/updated/or deleted in a trigger.
HLGEM
+1  A: 

You can add PRINT statements to the trigger and manually insert from ManagementStudio/Enterprise Manager to see where the trigger fails.

I see a problem when you insert multiple records in a single statement, as the SELECT FROM Inserted will return more than 1 record.

You can also update the SET statement to SELECT @Var = AllWos FROM Inserted

devio
Good call on the SET statement. The app (not mine) will only insert one record at time. I didn't worry about more than one row.
NitroxDM
You can't rely on that, databases are affected by more than the app.
HLGEM
+1  A: 

Is there a typo in the SET part of the AFTER trigger? You're selecting the same thing into three different variables.

Rather than confirming the behavior of Z_UpdateStageTable, I'd try to replace it with something dirt simple (a parameterless sql statement, say) to test whether the trigger's being called. It's possible that the sproc's not being called with what you think it's being called with.

MNGwinn
D’oh! I just saw that. Thanks!
NitroxDM