SQL trigger example

Source: Internet
Author: User

Inserted and deleted are two temporary tables used in the trigger. When the insert operation is executed, the inserted records are stored in inserted. When the delete operation is executed, the deleted records are stored in deleted. When update is executed, the modified records are stored in inserted and the pre-modified records are stored in deleted.
Copy codeThe Code is as follows:
Set ANSI_NULLS ON
Set QUOTED_IDENTIFIER ON
Go

Alter trigger [dnt_user_add]
ON [dbo]. [dnt_users]
AFTER INSERT
AS
BEGIN
Set nocount on;
Insert into [dnt_userfields] (uid)
Select uid from inserted

END


Delete:
Copy codeThe Code is as follows:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create trigger [dnt_users_del]
ON [dnt_users]
AFTER DELETE
AS
BEGIN

Set nocount on;
Delete [dnt_userfields] from deleted where [dnt_userfields]. uid = deleted. uid

END
GO

Modify:
Copy codeThe Code is as follows:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Alter trigger [dnt_users_up]
ON [dnt_users]
AFTER UPDATE
AS
BEGIN

Set nocount on;
UPDATE [dnt_userfields] Set icq = inserted. uid from inserted where [dnt_userfields]. uid = inserted. uid

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.