您好,登錄后才能下訂單哦!
小編給大家分享一下SQL Server觸發器有什么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
觸發器對表進行插入、更新、刪除的時候會自動執行的特殊存儲過程。觸發器一般用在check約束更加復雜的約束上面。觸發器和普通的存儲過程的區別是:觸發器是當對某一個表進行操作。諸如:update
、insert
、delete
這些操作的時候,系統會自動調用執行該表上對應的觸發器。SQL Server 2005
中觸發器可以分為兩類:DML觸發器和DDL觸發器,其中DDL觸發器它們會影響多種數據定義語言語句而激發,這些語句有create
、alter
、drop
語句。
a、 insert觸發器
b、 update觸發器
c、 delete觸發器
其中after觸發器要求只有執行某一操作insert、update、delete之后觸發器才被觸發,且只能定義在表上。而instead of
觸發器表示并不執行其定義的操作(insert
、update
、delete
)而僅是執行觸發器本身。既可以在表上定義instead of
觸發器,也可以在視圖上定義。
觸發器有兩個特殊的表:插入表(instered
表)和刪除表(deleted表)。這兩張是邏輯表也是虛表。有系統在內存中創建者兩張表,不會存儲在數據庫中。而且兩張表的都是只讀的,只能讀取數據而不能修改數據。這兩張表的結果總是與被改觸發器應用的表的結構相同。當觸發器完成工作后,這兩張表就會被刪除。Inserted
表的數據是插入或是修改后的數據,而deleted表的數據是更新前的或是刪除的數據。
對表的操作 | Inserted邏輯表 | Deleted邏輯表 |
增加記錄(insert) | 存放增加的記錄 | 無 |
刪除記錄(delete) | 無 | 存放被刪除的記錄 |
修改記錄(update) | 存放更新后的記錄 | 存放更新前的記錄 |
Update數據的時候就是先刪除表記錄,然后增加一條記錄。這樣在inserted
和deleted
表就都有update
后的數據記錄了。注意的是:觸發器本身就是一個事務,所以在觸發器里面可以對修改數據進行一些特殊的檢查。如果不滿足可以利用事務回滾,撤銷操作。
語法
create trigger tgr_name on table_name with encrypion –加密觸發器 for update... as Transact-SQL
--創建insert插入類型觸發器 if (object_id('tgr_classes_insert', 'tr') is not null) drop trigger tgr_classes_insert go create trigger tgr_classes_insert on classes for insert --插入觸發 as --定義變量 declare @id int, @name varchar(20), @temp int; --在inserted表中查詢已經插入記錄信息 select @id = id, @name = name from inserted; set @name = @name + convert(varchar, @id); set @temp = @id / 2; insert into student values(@name, 18 + @id, @temp, @id); print '添加學生成功!'; go --插入數據 insert into classes values('5班', getDate()); --查詢數據 select * from classes; select * from student order by id;
insert
觸發器,會在inserted
表中添加一條剛插入的記錄。
--delete刪除類型觸發器 if (object_id('tgr_classes_delete', 'TR') is not null) drop trigger tgr_classes_delete go create trigger tgr_classes_delete on classes for delete --刪除觸發 as print '備份數據中……'; if (object_id('classesBackup', 'U') is not null) --存在classesBackup,直接插入數據 insert into classesBackup select name, createDate from deleted; else --不存在classesBackup創建再插入 select * into classesBackup from deleted; print '備份數據成功!'; go -- --不顯示影響行數 --set nocount on; delete classes where name = '5班'; --查詢數據 select * from classes; select * from classesBackup;
delete
觸發器會在刪除數據的時候,將剛才刪除的數據保存在deleted
表中。
--update更新類型觸發器 if (object_id('tgr_classes_update', 'TR') is not null) drop trigger tgr_classes_update go create trigger tgr_classes_update on classes for update as declare @oldName varchar(20), @newName varchar(20); --更新前的數據 select @oldName = name from deleted; if (exists (select * from student where name like '%'+ @oldName + '%')) begin --更新后的數據 select @newName = name from inserted; update student set name = replace(name, @oldName, @newName) where name like '%'+ @oldName + '%'; print '級聯修改數據成功!'; end else print '無需修改student表!'; go --查詢數據 select * from student order by id; select * from classes; update classes set name = '五班' where name = '5班';
update
觸發器會在更新數據后,將更新前的數據保存在deleted
表中,更新后的數據保存在inserted
表中。
if (object_id('tgr_classes_update_column', 'TR') is not null) drop trigger tgr_classes_update_column go create trigger tgr_classes_update_column on classes for update as --列級觸發器:是否更新了班級創建時間 if (update(createDate)) begin raisError('系統提示:班級創建時間不能修改!', 16, 11); rollback tran; end go --測試 select * from student order by id; select * from classes; update classes set createDate = getDate() where id = 3; update classes set name = '四班' where id = 7;
更新列級觸發器可以用update
是否判斷更新列記錄;
instead of
觸發器表示并不執行其定義的操作(insert
、update
、delete
)而僅是執行觸發器本身的內容。
創建語法:
create trigger tgr_name on table_name with encryption instead of update... as T-SQL
if (object_id('tgr_classes_inteadOf', 'TR') is not null) drop trigger tgr_classes_inteadOf go create trigger tgr_classes_inteadOf on classes instead of delete/*, update, insert*/ as declare @id int, @name varchar(20); --查詢被刪除的信息,病賦值 select @id = id, @name = name from deleted; print 'id: ' + convert(varchar, @id) + ', name: ' + @name; --先刪除student的信息 delete student where cid = @id; --再刪除classes的信息 delete classes where id = @id; print '刪除[ id: ' + convert(varchar, @id) + ', name: ' + @name + ' ] 的信息成功!'; go --test select * from student order by id; select * from classes; delete classes where id = 7;
if (object_id('tgr_message', 'TR') is not null) drop trigger tgr_message go create trigger tgr_message on student after insert, update as raisError('tgr_message觸發器被觸發', 16, 10); go --test insert into student values('lily', 22, 1, 7); update student set sex = 0 where name = 'lucy'; select * from student order by id;
alter trigger tgr_message on student after delete as raisError('tgr_message觸發器被觸發', 16, 10); go --test delete from student where name = 'lucy';
--禁用觸發器 disable trigger tgr_message on student; --啟用觸發器 enable trigger tgr_message on student;
--查詢已存在的觸發器 select * from sys.triggers; select * from sys.objects where type = 'TR'; --查看觸發器觸發事件 select te.* from sys.trigger_events te join sys.triggers t on t.object_id = te.object_id where t.parent_class = 0 and t.name = 'tgr_valid_data'; --查看創建觸發器語句 exec sp_helptext 'tgr_message';
if ((object_id('tgr_valid_data', 'TR') is not null)) drop trigger tgr_valid_data go create trigger tgr_valid_data on student after insert as declare @age int, @name varchar(20); select @name = s.name, @age = s.age from inserted s; if (@age < 18) begin raisError('插入新數據的age有問題', 16, 1); rollback tran; end go --test insert into student values('forest', 2, 0, 7); insert into student values('forest', 22, 0, 7); select * from student order by id;
if (object_id('log', 'U') is not null) drop table log go create table log( id int identity(1, 1) primary key, action varchar(20), createDate datetime default getDate() ) go if (exists (select * from sys.objects where name = 'tgr_student_log')) drop trigger tgr_student_log go create trigger tgr_student_log on student after insert, update, delete as if ((exists (select 1 from inserted)) and (exists (select 1 from deleted))) begin insert into log(action) values('updated'); end else if (exists (select 1 from inserted) and not exists (select 1 from deleted)) begin insert into log(action) values('inserted'); end else if (not exists (select 1 from inserted) and exists (select 1 from deleted)) begin insert into log(action) values('deleted'); end go --test insert into student values('king', 22, 1, 7); update student set sex = 0 where name = 'king'; delete student where name = 'king'; select * from log; select * from student order by id;
看完了這篇文章,相信你對“SQL Server觸發器有什么用”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。