6. Trigger operation:
--1. Create a trigger create trigger tesetTrigger -- trigger name on employee -- on which table the trigger is defined for insert -- trigger type as -- program statement begin print 'tesetTrigger is used'; end --2, call insert into employee(empName,empAge,empPay,empJob) values('Mr. Li',25,2500,'Senior Lecturer') --3. Specify the trigger sequence of the trigger: only the first and last one can be specified, and the random trigger in the middle --Create 6 insert triggers first declare @count int; declare @c int; declare @triggerName varchar(50); declare @sql varchar(100); declare @msg varchar(50); set @count = 6; set @c = 1; while(@c<=@count) begin set @triggerName = 'tesetTrigger'+cast(@c as varchar(5)); set @msg = @triggerName+'trigger was called'; set @sql='create trigger '+@triggerName+' on employee for insert as begin print '''+@msg+''' end '; set @sql='create trigger '+@triggerName+' on employee for insert as begin print '''+@msg+''' end '; print @sql; exec(@sql); set @c = @c+1; end --Execute the insert statement to view the calling sequence: tesetTrigger1, tesetTrigger2, tesetTrigger3, tesetTrigger4, tesetTrigger5, tesetTrigger6 triggers are called in sequence insert into employee(empName,empAge,empPay,empJob) values('Teacher He',35,7500,'Senior Lecturer'); --Adjust the execution process of the trigger: only the first and last ones can be adjusted, the first one is defined as tesetTrigger6, and the last one is defined as tesetTrigger1 exec sp_settriggerorder @triggername='tesetTrigger6',@order='first',@stmttype='insert'; exec sp_settriggerorder @triggername='tesetTrigger1',@order='last',@stmttype='insert'; --Execute sql to view the sequence: tesetTrigger6, tesetTrigger2, tesetTrigger3, tesetTrigger4, tesetTrigger5, tesetTrigger1 triggers are called in sequence insert into employee(empName,empAge,empPay,empJob) values('Mr. Xiaolan',20,3500,'Assistant'); --Cancel the execution order specified by the trigger exec sp_settriggerorder @triggername='tesetTrigger6',@order='none',@stmttype='insert'; exec sp_settriggerorder @triggername='tesetTrigger1',@order='none',@stmttype='insert'; --Execute the insert statement to view the calling sequence: tesetTrigger1, tesetTrigger2, tesetTrigger3, tesetTrigger4, tesetTrigger5, tesetTrigger6 triggers are called in sequence insert into employee(empName,empAge,empPay,empJob) values('Mr. Xiaojin',24,3500,'Assistant'); --sp_settriggerorder trigger structure: exec sp_settriggerorder @triggername='trigger name',@order='first|last|none',@stmttype='trigger type, insert|update|delete optional'; --4, instead of triggers --Create a for delete trigger first create trigger tesetDeleteTrigger on employee for delete as begin print 'tesetDeleteTrigger was called'; end --Execute the delete operation to view the execution: data is deleted and the console prints "tesetDeleteTrigger trigger was called" delete from employee where empId=15; --Create another delete trigger instead of create trigger tesetInsteadOfDeleteTrigger on employee instead of delete as begin print 'The alternative trigger tesetInsteadOfDeleteTrigger was called'; end --Execute the delete operation to view the execution: the console prints "the alternative trigger tesetInsteadOfDeleteTrigger was called" but the data is not deleted delete from employee where empId=16; --Compare: --for trigger: directly manipulate data, such as adding, deleting data, etc., the data in the database changes after execution --instead of trigger: do not operate the data, just perform the "operation specified in the trigger", and the data does not change --5, view the trigger SELECT name, definition FROM sys.sql_modules AS m INNER JOIN sys.all_objects AS o ON m.object_id = o.object_id WHERE o.[type] = 'tr' --6, modify the trigger --The modification is consistent with the creation syntax structure, that is, the created keyword can be modified to the alter keyword. As for the business logic, it can be modified according to the actual situation. ALTER trigger tesetDeleteTrigger on employee for insert,update,delete as begin print 'The tesetDeleteTrigger trigger was called after modification'; end --7, delete trigger drop trigger tesetDeleteTrigger; --8. View the basic information of the trigger, including name, owner, type and creation time exec sp_help tesetDeleteTrigger; --9. View the trigger creation code exec sp_helptext tesetDeleteTrigger; --Note: If all the code when creating a new trigger is one line (such as tesetTrigger1), then the found code is one line. If the format is set such as tesetDeleteTrigger, then the code is displayed line by line as it is newly created.