Triggers implement field changes to the inserted data Oracle+sql Server

Source: Internet
Author: User

There has recently been a need to use triggers to make changes to a column of the inserted data, thus collating the trigger notation of Oracle and SQL Server for such requirements, and this article only mentions insert triggers.

First we create a table:

--Creating Test Table CREATE TABLE test (ID int primary key,name varchar), sex varchar (1), status int)

Our goal is to implement the requirement to set the status to 1 when the newly inserted data satisfies the sex field null, and to change the conditions according to the actual needs.

So SQL Server has the following syntax:

Create trigger [dbo]. [TRG1] on [dbo]. [Test] for insert as Declare C cursor to select Id,sex from inserteddeclare @id intdeclare @sex varchar (1) Open Cfetch NEX T from C to @id, @sexWhile @ @FETCH_STATUS = 0Beginif @sex is nullupdate dbo.test set Status=1 where [email protected]
   
    --This is updated with the primary key to maximize efficiency fetch NEXT from C to @id, @sexENDCLOSE cdeallocate C
   

Oracle's notation is:

Create or replace trigger TRG1 before insert on testreferencing new as new old as oldfor each rowbegin    if:old.sex is Null then    : new.status:=1;    End If; End Trg1;

You can also implement the same functionality.

Summary and comparison:

We found that because Oracle provides BEFORE/AFTER trigger notation so it's simpler to implement, and SQL Server relies on cursors only, it's clear that Oracle's approach is simpler and clearer.

However, it is not said that Oracle's syntax is better in all cases, and that sometimes SQL Server syntax can implement features that are difficult for Oracle triggers to implement.

Triggers implement field changes to the inserted data Oracle+sql Server

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.