"2018-01-19" SQL server-Trigger

Source: Internet
Author: User

1 --CREATE table users--Creating user tables2 --(3 --IDs int primary key identity (.),4 --name nvarchar,--names5 --class nvarchar (200)--Class number6 --)7 8 --CREATE table class--Creating a class table9 --(Ten --classcode nvarchar ($) primary key,--class number One --className nvarchar (,--class name) A --) -  - ----Insert class table data the --INSERT into class values (' c001 ', ' one Shift '); - --INSERT into class values (' c002 ', ' Class Two '); - --INSERT into class values (' c003 ', ' class Three '); - --INSERT into class values (' c004 ', ' Class Four '); + --INSERT into class values (' c005 ', ' five classes '); - ----Insert User table data + --INSERT into users values (' Zhang Yi ', ' c001 '); A --INSERT into users values (' Zhang Yi ', ' c002 '); at --INSERT into users values (' Zhang San ', ' c003 '); - --INSERT into users values (' Zhang Si ', ' c004 '); - --INSERT into users values (' Five ', ' c005 '); -  - --select * from users; - --SELECT * from class; in  - ----Trigger to ----(a special stored procedure, no way to call it directly, but by adding or deleting the action to trigger it + ----One action on a table can only have one trigger) - Create TriggerUsers_delete the  onUsers *  for Delete--perform a delete operation to trigger, and then perform the following actions $  asPanax Notoginseng Select *  fromUsers -  the  + Create TriggerUsers_insert A  onUsers the  for Insert--perform an insert operation to trigger and then perform the Operation +  as - Select *  fromUsers $  $ Create TriggerUsers_delete -  onUsers -instead of Delete--When you perform a delete operation, the operation is not performed and the following actions replace the  as - Select *  fromUsersWuyi  the Create TriggerUsers_delete -  onUsers Wuinstead of Delete -  as About Select *  fromDeleted--querying for deleted data $  -  - Create TriggerUsers_delete -  onUsers Ainstead of Delete +  as the     Declare @a nvarchar( -) -     Select @a=Ids fromdeleted $     if @a=2 the     begin the         Select 'It 's too ugly to be deleted! ' the     End the     Else -     begin in         Delete  fromUserswhereIds=@a the     End the      About Delete  fromUserswhereIds=2 the  the  the  + ----Cascade Delete - Create TriggerClass_delete the  onclassBayiinstead of Delete the  as the     Declare @a nvarchar( $); -     Select @a=Classcode fromdeleted; -     Delete  fromUserswhereClass=@a; the     Delete  fromClasswhereClasscode=@a; the      the Delete  fromClasswhereClasscode='c001';

"2018-01-19" SQL server-Trigger

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.