SQL Server triggers

Source: Internet
Author: User
A trigger is a special type of stored procedure, which is different from the stored procedure we introduced earlier. A trigger is triggered by an event and automatically called for execution. The stored procedure can be called by the name of the stored procedure. What is a special stored procedure that is automatically executed when a trigger inserts, updates, or deletes a table. Trigger

A trigger is a special type of stored procedure, which is different from the stored procedure we introduced earlier. A trigger is triggered by an event and automatically called for execution. The stored procedure can be called by the name of the stored procedure. What is a special stored procedure that is automatically executed when a trigger inserts, updates, or deletes a table. Trigger

A trigger is a special type of stored procedure, which is different from the stored procedure we introduced earlier. A trigger is triggered by an event and automatically called for execution. The stored procedure can be called by the name of the stored procedure.

Ø what is a trigger?

A special stored procedure that is automatically executed when a trigger inserts, updates, or deletes a table. Triggers are generally used for constraints with more complex check constraints. The difference between a trigger and a common stored procedure is that a trigger is used to operate a table. For example, update, insert, and delete operations, the system automatically calls the trigger for executing the table. In SQL Server 2005, triggers can be divided into two types: DML triggers and DDL triggers. DDL triggers affect the execution of multiple data definition language statements, including create, alter, and drop statements.

DML triggers are divided:

1. after trigger (triggered later)

A. insert trigger

B. update trigger

C. delete trigger

2. instead of trigger (previously triggered)

The after trigger requires that the trigger be triggered only after an insert, update, or delete operation is executed and can only be defined on the table. The instead of trigger does not execute the defined operations (insert, update, delete) but only executes the trigger itself. You can either define an instead trigger on a table or a view.

The trigger has two special tables: The Insert table and the delete table ). These two tables are logical tables and virtual tables. Two tables created by the system in the memory are not stored in the database. Both tables are read-only and can only read data but cannot modify data. The results of these two tables are always the same as the structure of the table applied by the modified trigger. After the trigger is complete, the two tables will be deleted. The data in the Inserted Table is the Inserted or modified data, and the data in the deleted table is the data before or after the update.

Table operations

Inserted logical table

Deleted logical table

Insert)

Store added records

None

Delete)

None

Store deleted records

Update)

Store updated records

Store records before update

When updating data, you delete the table record and add a record. In this way, the inserted and deleted tables have updated data records. Note: The trigger itself is a transaction, so you can perform some special checks on the modified data in the trigger. If not, you can use transaction rollback to cancel the operation.

Create a trigger

Syntax

...
As
Transact-SQL

# Create an insert trigger

-- Create an insert trigger
)
Tgr_classes_insert
Id;

Insert trigger. A newly inserted record is added to the inserted Table.

# Create a delete trigger

-- Delete trigger
)
Tgr_classes_delete
;
)
-- ClassesBackup exists to insert data directly.
Insert into classesBackup select name, createDate from deleted;
Else
-- ClassesBackup does not exist before creation and insertion
Select * into classesBackup from deleted;
Print 'data backed up successful! ';
Go
--
-- The number of affected rows is not displayed.
-- Set nocount on;
Delete classes where name = 'class 5 ';
-- Query data
Select * from classes;
Select * from classesBackup;

The delete trigger will save the deleted data in the deleted table When deleting the data.

# Create an update trigger

-- Update type trigger
)
Tgr_classes_update
+ @ OldName +;
Id;
Select * from classes;
Update classes set name = 'five class' where name = 'five class ';

After the update trigger updates the data, it saves the pre-update data in the deleted table and the updated data in the inserted Table.

# Update a column-Level Trigger

)
Tgr_classes_update_column
;
Id = 7;

You can use update to determine whether to update Column records;

# Instead of Type trigger

The instead of trigger indicates that the operation defined by the trigger is not executed (insert, update, delete), but only the content of the trigger itself.

Create syntax

...
As
T-SQL

# Create an instead of trigger

)
Tgr_classes_inteadOf

+ Convert (varchar, @ id) + ', name:' + @ name + '] Information successful! ';
Go
-- Test
Select * from student order by id;
Select * from classes;
Delete classes where id = 7;

# Display custom message raiserror

)
Tgr_message
Id;

# Modify a trigger


Student where name = 'Lucy ';

# Enable or disable a trigger

-- Disable a trigger
Disable trigger tgr_message on student;
-- Enable trigger
Enable trigger tgr_message on student;

# Querying created trigger information

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.