Similar to the before trigger in Oracle in SQL Server

Source: Internet
Author: User

In this case, in addition to enabling cascading modification and cascading deletion using the primary and foreign key constraints between two tables, we can also use triggers to accomplish similar functions. The following example shows how to delete a trigger:

 

Assume that a sysfuncdic (function dictionary table), sysfuncrights (function permission table), funcid in the function permission table, and funcid in the function dictionary table have foreign key constraints. Now we want to delete the records in sysfuncdic, it cannot be deleted because it has been used as a foreign key constraint. The solution is to delete all records corresponding to funcid in the sysfuncrights table.

 

Create Table sysfuncdic (funcid int not null identity (100) primary key, funcname varchar () Create Table sysfuncrights (ID int not null Primary Key Identity ), funcid int not null, empid int not null) insert into sysfuncdic values ('A') insert into sysfuncdic values ('bb') insert into sysfuncdic values ('cc ') insert into sysfuncrights values) /*********************** delete a trigger ************** * ***********/create trigger tri_delete on sysfuncdicinstead of deleteasbegin declare @ funcid int; select @ funcid = funcid from deleted; Delete sysfuncdic where funcid = @ funcid; Delete sysfuncrights where funcid = @ funcid; end /************************************** * ******************/select * From sysfuncdic; -- query function dictionary table record select * From sysfuncrights; -- query function permission Table Record Delete from sysfuncdic where funcid = 1; -- delete function dictionary table record for test

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.