SQL Server 2005 用觸發器跟蹤表操作

--> 測試數據:[a]
if object_id('[a]') is not null drop table [a]
go 
create table [a]([ID] int,[品名] varchar(6),[入庫數量] int,[入庫時間] datetime)--入庫時間爲被跟蹤字段
insert [a]
select 1,'礦泉水',100,'2013-01-02' union all
select 2,'方便麪',60,'2013-01-03' union all
select 3,'方便麪',50,'2013-01-03' union all
select 4,'礦泉水',80,'2013-01-04' union all
select 5,'方便麪',50,'2013-01-05'

select * from a

ID          品名     入庫數量        入庫時間
----------- ------ ----------- -----------------------
1           礦泉水    100         2013-01-02 00:00:00.000
2           方便麪    60          2013-01-03 00:00:00.000
3           方便麪    50          2013-01-03 00:00:00.000
4           礦泉水    80          2013-01-04 00:00:00.000
5           方便麪    50          2013-01-05 00:00:00.000

(5 行受影響)

建立測試數據 表A。

-- 建跟蹤表
create table log_sto
(logid int not null identity(1,1),  -- 日誌序號(日誌主鍵)
 operate varchar(10),               -- 操作類型 如Insert,Update,Delete.
 id int,                            -- 原表ID(主鍵) 
 old_de datetime,                   -- de字段舊值
 new_de datetime,                   -- de字段新值
 spid int not null,                 -- spid
 login_name varchar(100),           -- 登錄名
 prog_name varchar(100),            -- 程序名
 hostname varchar(100),             -- 主機名
 ipaddress varchar(100),            -- IP地址
 runsql varchar(4000),              -- 執行的TSQL代碼
 UDate datetime                     -- 操作日期時間
 constraint pk_logsto primary key(logid)
)
建立跟蹤日誌表 log_sto
-- 爲a表建觸發器
create trigger tr_sto
on a after update,insert,delete
as
begin
   declare @di table(et varchar(200),pt varchar(200),ei varchar(max))
   insert into @di exec('dbcc inputbuffer(@@spid)')
   
   declare @op varchar(10)
   select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
                   then 'Update'
                   when exists(select 1 from inserted) and not exists(select 1 from deleted)
                   then 'Insert'
                   when not exists(select 1 from inserted) and exists(select 1 from deleted)
                   then 'Delete' end
                   
   if @op in('Update','Insert')
   begin
   insert into log_sto
     (operate,id,old_de,new_de,spid,login_name,prog_name,hostname,ipaddress,runsql,UDate)
     select @op,n.id,o.[入庫時間],n.[入庫時間],@@spid,
       (select login_name from sys.dm_exec_sessions where session_id=@@spid),
       (select program_name from sys.dm_exec_sessions where session_id=@@spid),
       (select hostname from sys.sysprocesses where spid=@@spid),
       (select client_net_address from sys.dm_exec_connections where session_id=@@spid),
       (select top 1 isnull(ei,'') from @di),
       getdate()
     from inserted n
     left join deleted o on o.id=n.id
   end
   else
   begin
     insert into log_sto
       (operate,id,old_de,new_de,spid,login_name,prog_name,hostname,ipaddress,runsql,UDate)
       select @op,o.id,o.[入庫時間],null,@@spid,
         (select login_name from sys.dm_exec_sessions where session_id=@@spid),
         (select program_name from sys.dm_exec_sessions where session_id=@@spid),
         (select hostname from sys.sysprocesses where spid=@@spid),
         (select client_net_address from sys.dm_exec_connections where session_id=@@spid),
         (select top 1 isnull(ei,'') from @di),
         getdate()
       from deleted o
   end
end
go
爲a表建立觸發器

--插入測試數據
insert [a]
select 1,'礦泉水',100,'2013-01-02' union all
select 2,'方便麪',60,'2013-01-03' union all
select 3,'方便麪',50,'2013-01-03' union all
select 4,'礦泉水',80,'2013-01-04' union all
select 5,'方便麪',50,'2013-01-05'
DBCC 執行完畢。如果 DBCC 輸出了錯誤信息,請與系統管理員聯繫。

(1 行受影響)

(5 行受影響)

(5 行受影響)

select * from a 

ID          品名     入庫數量        入庫時間
----------- ------ ----------- -----------------------
1           礦泉水    100         2013-01-02 00:00:00.000
2           方便麪    60          2013-01-03 00:00:00.000
3           方便麪    50          2013-01-03 00:00:00.000
4           礦泉水    80          2013-01-04 00:00:00.000
5           方便麪    50          2013-01-05 00:00:00.000
1           礦泉水    100         2013-01-02 00:00:00.000
2           方便麪    60          2013-01-03 00:00:00.000
3           方便麪    50          2013-01-03 00:00:00.000
4           礦泉水    80          2013-01-04 00:00:00.000
5           方便麪    50          2013-01-05 00:00:00.000

(10 行受影響)

插入測試數據

--查詢跟蹤表
select * from log_sto

更新和刪除操作同樣可以監控數據。

發佈了50 篇原創文章 · 獲贊 7 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章