SQL Server, sqlserver
- A trigger is an addition, deletion, and modification operation on a table, which causes or triggers the addition, deletion, and modification operation on another table. Therefore, the trigger has three types: deleted trigger, Update trigger, and insert trigger.
- The trigger adds, deletes, and modifies the trigger based on the original addition, deletion, and modification operations, it can be divided into the Instead trigger and the For or After trigger (for and after belong to a trigger)
- The use of the trigger involves two very important tables used to save records that have been changed or that are not found in the table operated in the first chapter, namely the Virtual table Inserted and the virtual table Deleted.
|
Virtual table Inserted |
Virtual table Deleted |
When a table record is added |
Store new records |
Do not store records |
Modification time |
Store new records for update |
Store records before update |
Deleting |
Do not store records |
Store deleted records |
- The following is an easy-to-understand small example of a trigger used in restructuring the Personal Edition of the data room charging system.
- When we delete the active working records in the T_OnWork_info table, we also write the work records to the T_Work_info table, and change the IsOn field in the T_Admin_info table to False.
Create trigger trigger_delOnWork ON T_OnWork_info for delete as declare @ name varchar (10) DECLARE @ loginDate varchar (10) DECLARE @ loginTime varchar (10) DECLARE @ logoutDate varchar (10) DECLARE @ logoutTime varchar (10) DECLARE @ comName varchar (10) SELECT @ name = (select name from deleted) SELECT @ loginDate = (SELECT loginDate FROM deleted) ----- the deleted table is used to store the deleted records in the T_OnWork table. SELECT @ loginTime = (sELECT loginTime FROM deleted) SELECT @ comName = (SELECT comName FROM deleted) SET @ logoutDate = convert (varchar (10), getdate (), 101) SET @ logoutTime = convert (varchar (10), getdate (), 108) if (select count (*) FROM deleted) = 1 begin insert into T_Work_info (Name, loginDate, loginTime, logoutDate, logoutTime, comName) VALUES (@ name, @ loginDate, @ loginTime, @ logoutDate, @ logoutTime, @ comName) UPDATE T_Admin_info SET IsOn = 'false' WHERE Name = (SELECT Name FROM deleted) rollback tran -- transaction ROLLBACK END
How about the trigger? The trigger is simple. You can try to use it. The trigger + transaction rollback mechanism can easily reduce the burden on the code.
SQL _server_2000 suspended
1. Enter regedit in start-> running
2. to the HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager location
3. Select File> export and save (for security reasons)
4. Right-click PendingFileRenameOperations in the right window, select Delete, and then confirm
5. Delete Microsoft SQL Server in C: \ Program Files
6. Delete Microsoft SQL Server files for installing other disks
That's simple ~~~
SQL _server_2000 suspended
1. Enter regedit in start-> running
2. to the HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager location
3. Select File> export and save (for security reasons)
4. Right-click PendingFileRenameOperations in the right window, select Delete, and then confirm
5. Delete Microsoft SQL Server in C: \ Program Files
6. Delete Microsoft SQL Server files for installing other disks
That's simple ~~~