create TRIGGER [dbo].[table_Logger] ON [dbo].table
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
SET NOCOUNT ON;
declare @Actiune varchar(50),
@HostName varchar(50),
@SQL nvarchar(500),
@d int,
@i int,
@event int
set @HostName = null
select @HostName = hostname
from master..sysprocesses
where loginame = user_name()
select @HostName = isnull(@HostName, '?'), @d = 0, @i = 0
if exists (select top 1 1 from inserted)
select @i = 1
if exists (select top 1 1 from deleted)
select @d = 1
if @d=1 and @i=1
select @event = 1 /*update*/
else
begin
if @i=1 and @d = 0
select @event = 2 /*insert*/
if @d = 1 and @i = 0
select @event = 3 /*delete*/
end
if @event = 1
begin
insert into table_LOG
select *, 'deleted' , system_user, 'update', getdate(), @HostName
from deleted
insert into table_LOG
select *, 'inserted' , system_user, 'update', getdate(), @HostName
from inserted
end
else if @event = 2
insert into OJ_GrupuriDetaliiSuplimentare_LOG
select *, 'inserted' , system_user, 'insert', getdate(), @HostName
from inserted
else if @event = 3
insert into table_LOG
select *, 'deleted' , system_user, 'delete', getdate(), @HostName
from deleted
end
Replace Table With your table
The log table has the following structure:
create table "table_LOG"
(
.............
[TabelSursa] [varchar](100) NULL,
[NumeUtilizatorLog] [varchar](100) NULL,
[Actiune] [varchar](20) NULL,
[DataLog] [datetime] NULL,
[HostName] [varchar](50) NULL
)
Wednesday, January 4, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment