How to get table_name in a trigger - SQL Server -
i've created trigger task store info trigger-events. example: "new employee added table on date 2014-10-13.
i've created table - audit - stores info (from trigger).
create table [dbo].[audit]( [id] [int] identity(1,1) not null, [tablename] [nvarchar](255) not null, [auditdata] [nvarchar](max) null, [username] [nvarchar](255) not null, primary key (id))
however, trigger i've created looks this:
create trigger [dbo].[tr_actor_forinsert_audit] on [dbo].[actor] insert begin declare @username nvarchar(255) declare @tablename nvarchar(255) = 'actor' declare @name varchar(255) declare @birthdate date select @username = system_user select @name = name inserted select @birthdate = birthdate inserted insert audit values (@tablename, 'new ' + lower(@tablename) + ' name = ' + @name + ' , birthdate = ' + convert(nvarchar,@birthdate) + ' added @ ' + convert(nvarchar,getdate()), @username)
end;
as can see, variable username initialized system_user. variable tablename intitialized hard-coded value .
question: there possible way somehow generically initialized variable tablename tablename same way did username?
for example, if existed:
@tablename = system_table_where_trigger(triggername)_exist
regards,
christian
trigger intended fixed use.however if want dynamic, phone call sp within it.
call stored procedure within create trigger in sql server
sql-server triggers audit
No comments:
Post a Comment