Sunday, September 5, 2010

How to track if any table is deleted/drop from SQL Server database by user

Today I will post one more interesting thing in sql server database.i.e. How to track if any table is deleted/drop from SQL Server database by user….

use DB_name

CREATE TABLE create_table_log
( create_time datetime
, DB_User nvarchar(100)
, Event_type nvarchar(100)
, TSQL nvarchar(2000));
use Db_name
go
create trigger trig_create_table on database for create_table
as
Declare @data xml
set @data=Eventdata()
Insert into create_table_log values
(getdate(),
@data.value('(/EVENT_INSTANCE/LOGINNAME)[1]','nvarchar(100)'),
@data.value('(/EVENT_INSTANCE/EVENTTYPE)[1]','nvarchar(100)'),
@data.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(200)')
)

No comments: