SQL Server-triggers for code offloading

Source: Internet
Author: User

  1. trigger is a table of additions and deletions to the operation, causing or triggering the addition and deletion of another table operation, so the trigger has 3 deleted trigger, update trigger, insert trigger
  2. triggers are replaced by The original addition and deletion of the operation, or in the original additions and deletions after the completion of the modification operation, divided into triggers and for or after Trigger ( for and after belongs to a trigger)
  3. the use of triggers involves two very important tables that are used to hold records that have changed or that are not present on the table in which the first chapter was manipulated, namely virtual tables Inserted and Virtual Tables Deleted

Virtual table inserted

  Virtual table deleted

Store the new record  

Do not store records

Store new records for update

Store pre-update records

When deleted

Do not store records

To store deleted records

  1. here is an easy-to-understand example of a trigger that is used to reconstruct a personal version of a computer room fee system .
    1. before we removeT_onwork_infowhen working on a machine in a table,T_work_infotable to write the work record, and alsoT_admin_infoin the tableIsOnfield is changed toFalse
      CREATE TRIGGER Trigger_delonwork on T_onwork_info for DELETE as DECLARE @name varchar (TEN) DECLARE @loginDate varchar (TEN) DECLARE @loginTim e varchar (DECLARE) @logoutDate varchar (DECLARE @logoutTime varchar) DECLARE @comName varchar () SELECT @name = ( Select NAME from deleted) Select @loginDate = (select logindate from deleted)-----deleted table is used to store the deleted records in T_onwork table SELECT @lo Gintime = (select Logintime from deleted) SELECT @comName = (select Comname from deleted) SET @logoutDate =convert (varchar ( ), GETDATE (), 101) SET @logoutTime =convert (varchar (), GETDATE (), 108) if (SELECT COUNT (*) from deleted) =1 BEGIN INSERT I NTO 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 TRA N--Transaction rollback END 
      How about, the trigger is very simple, you can try to use, trigger+the rollback mechanism of a transaction makes it easy to offload the code

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.