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:
INSERTED
? SYS_CONTEXT('USERENV', 'SESSIONID')
? 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.