Trigger and trigger instance

Source: Internet
Author: User

Trigger and trigger instance

Although I have been familiar with triggers, I am not very thorough. Sometimes I still don't quite understand them. Now I want to add this blank space.

The trigger content is basically as follows:


Note: When a trigger is executed, the system creates one or two temporary tables, Inserted and Deleted. The user saves the new and old values of the modified rows. For insert operations, only the Inserted Table is generated and each newly Inserted data is saved. For delete operations, only the Deleted table is generated and each Deleted Data is saved; for the update operation, both the Inserted Table and the Deleted table are generated. The Deleted table stores the data before the update, and the Inserted Table stores the updated data. The two tables can be directly used in the trigger.

The following is a specific example:

Take the FOR trigger as an example:

For a login table, the original data is as follows:

The trigger is written as follows:

Execute the following statement and obtain the result as shown in:

Through the comparison of the preceding two results, the SQL statement changes the user password from administrator to operator, and the for trigger changes from 123 to lj. It is such a process.


SQL trigger update

You can use the updated field to execute the trigger.

CREATE TRIGGER GXDHSL ON RKD
FOR UPDATE
AS
IF (Update (field name ))
Begin

DECLARE @ dhdh varchar (50) -- Plan ticket number
DECLARE @ SL decimal () -- number before modification
DECLARE @ DHSL decimal () -- number after modification
SELECT @ DHDH = ysdh, @ SL = SSSL FROM DELETED
SELECT @ DHSL = SSSL FROM INSERTED
UPDATE GL_QGD set dhsl = DHSL-ISNULL (@ SL, 0) where dh = @ DHDH
End

Go

SQL trigger update

You can use the updated field to execute the trigger.

CREATE TRIGGER GXDHSL ON RKD
FOR UPDATE
AS
IF (Update (field name ))
Begin

DECLARE @ dhdh varchar (50) -- Plan ticket number
DECLARE @ SL decimal () -- number before modification
DECLARE @ DHSL decimal () -- number after modification
SELECT @ DHDH = ysdh, @ SL = SSSL FROM DELETED
SELECT @ DHSL = SSSL FROM INSERTED
UPDATE GL_QGD set dhsl = DHSL-ISNULL (@ SL, 0) where dh = @ DHDH
End

Go

Related Article

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.