SQL Server, sqlserver

Source: Internet
Author: User

SQL Server, sqlserver

  1. 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.
  2. 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)
  3. 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

 

  1. The following is an easy-to-understand small example of a trigger used in restructuring the Personal Edition of the data room charging system.
    1. 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 ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.