Friday, March 23, 2012
Insert Trigger sometimes not firing
i have an issue with an insert trigger sometimes not firing.
here is the trigger
CREATE TRIGGER Insert_tPABillToAddr ON [dbo].[tPA00175]
FOR INSERT
AS
INSERT into tPABillToAddr
(
chrJobNumber
)
SELECT chrJobNumber
FROM inserted
when the user enters a new this table is to insert one column into another table. the thing is, sometimes it does not do the insert. any ideas as to why? it is a very uncommon thing, lets say once out of every 20 inserts does it fail. but it is crucial that it never fails.
thanksYou can be sure the trigger is firing.
Are you sure the insert is occurring? Add a few more lines to the trigger so that data is also inserted into a log table indicating the datetime, login, and chrJobNumber of each insert.
Keep in mind that your trigger will not fire for updates that may modify an existing chrJobNumber value.|||what would be a probable cause for it not to fire? this is just an insert...i have no updates associated with it.|||While I've investigated a few cases where people thought that triggers didn't fire, I've only seen one or two cases in SQL 4.2 and SQL 6.0 where there were actually cases where a trigger should have fired, but didn't. Those were bugs in the database engine itself.
I haven't seen that happen since the release of SQL 6.5.
-PatP|||what if you have more than 1 insert trigger...lets say the other, for whatever reason doesnt fire, does it cause this one to cease as well?
Wednesday, March 7, 2012
Insert query firing Insert & Update trigger at the same time.
I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp.
Insert and Update trigger work fine when i have only one of them defined.
However when I have all the 3 triggers in place and when i try to fire a insert query on the statement. It triggers both insert and update trigger at the same time and has the same timestamp in the audit table.
Insert trigger goes as
CREATE TRIGGER InsRecord ON [dbo].[tableA]
AFTER INSERT
AS
insert Audit(change_id,change_table,change_type,date_chan ge)
select uniqueid, srctable,'Insert',GetDate() from inserted
Update trigger goes as
CREATE TRIGGER UpdRecord ON [dbo].[tableA]
FOR UPDATE
AS
insert Audit(change_id,change_table,change_type,date_chan ge)
select uniqueid, srctable,'Update',GetDate() from inserted
Delete Trigger goes as
CREATE TRIGGER delRecord ON [dbo].[tableA]
FOR DELETE
AS
insert Audit(change_id,change_table,change_type,date_chan ge)
select uniqueid, srctable,'Delete',GetDate() from deleted
Note:This tableA has relations with 2 other tables on 1 field each from each table but i don't think it should matter.
Please advise how to prevent it.CREATE TRIGGER alteredRecord ON [dbo].[tableA]
FOR INSERT, UPDATE, DELETE
AS
BEGIN
...declare lngIns & lngDel
SELECT lngIns=count(col1)
from inserted
select lngDel=count(col1)
from deleted
IF lngIns>0 and lngDel=0
...inserted
else if lngIns>0 and lngDel>0
...updated
else if lngIns=0 and lngDel>0
...deleted
end
END