简体   繁体   中英

Oracle AUDSID equivalent in SQL Server 2012

What is the equivalent SQL Server 2012 code for this please ?

 IF INSERTING THEN
            :NEW.audsid:=SYS_CONTEXT('USERENV', 'SESSIONID');

Oracle and SQL Server handle triggers very differently. Oracle has the concept that a trigger affects only one row at a time. SQL Server doesn't. Instead, it uses a "table" inserted with the new rows.

So, your question has three parts:

  1. What is the equivalent of INSERTED ?
  2. What is the equivalent of SYS_CONTEXT('USERENV', 'SESSIONID') ?
  3. What is the best way to do this in SQL Server?

Here is the answer to the first two questions:

if (exists (select 1 from inserted) and (not exists (select 1 from deleted))
    update inserted
        set audsid = @@SPID;

However, in most cases, you would just use the default keyword in the column definition:

audsid int default @@SPID

Much easier and a trigger isn't needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM