Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trigger for tracking changes in table
Message
From
21/06/2003 11:44:34
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Other
Title:
Trigger for tracking changes in table
Miscellaneous
Thread ID:
00802591
Message ID:
00802591
Views:
58
Hi all,
I have this trigger to track the changes in the table dbo.tbl_gc_CodZonas, but it creates two reg for update and insert, any chance to create only one for UPDATE and one for INSERT. I know if i have one for UPDATE one FOR INSERT and one for DELETE it works, but my database have about 150 tables, or is there any other way to do that?
CREATE TRIGGER SystemLog_CodZonas ON dbo.tbl_gc_CodZonas
FOR INSERT, UPDATE, DELETE
AS
DECLARE @TypeTrans CHAR(6) 
DECLARE @TRCodigo VARCHAR(16)
IF EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted) 
SET @TypeTrans = 'UPDATE' 
ELSE 
IF EXISTS(SELECT * FROM inserted)
SET @TypeTrans = 'INSERT' 
ELSE IF EXISTS(SELECT * FROM deleted)
SET @TypeTrans = 'DELETE'
IF @TypeTrans = 'INSERT' 
BEGIN
SET @TRCodigo = (SELECT c_codigo  FROM Inserted)
INSERT INTO dbo.tbl_system_log (c_tablename,c_accao,c_codigoalt,c_username, d_datetime)
VALUES ('dbo.tbl_gc_CodZonas','INSERT',@TRCodigo,suser_sname(), getdate())
END
IF @TypeTrans = 'UPDATE' 
BEGIN
SET @TRCodigo = (SELECT c_codigo  FROM Inserted)
INSERT INTO dbo.tbl_system_log (c_tablename,c_accao,c_codigoalt,c_username, d_datetime)
VALUES ('dbo.tbl_gc_CodZonas','UPDATE',@TRCodigo,suser_sname(), getdate())
END
IF @TypeTrans = 'DELETE' 
BEGIN
SET @TRCodigo = (SELECT c_codigo  FROM deleted)
INSERT INTO dbo.tbl_system_log (c_tablename,c_accao,c_codigoalt,c_username, d_datetime)
VALUES ('dbo.tbl_gc_CodZonas','DELETE',@TRCodigo,suser_sname(), getdate())
END
Thanks

Joao Batista
Next
Reply
Map
View

Click here to load this message in the networking platform