[英]Trigger From Oracle to SQL SERVER
嗨,我如何将这个触发器从oracle重写到sql server?
CREATE OR REPLACE TRIGGER COUNTER
BEFORE INSERT OR UPDATE OF some_column ON my_table
FOR EACH ROW
BEGIN
:NEW.My_counter := :NEW.My_counter+1;
:NEW.value := :NEW.value+1;
END;
谢谢您的帮助。
在这种情况下,Oracle绝对更优雅……您可以尝试以下方法:
CREATE TRIGGER counter_trigger
ON my_table AFTER INSERT, UPDATE
AS
BEGIN
update t
set t.my_counter +=1,
t.value += 1
from my_table t
where exists ( -- restrict to inserted or updated rows ...
select null
from inserted i
where i.Id = t.Id
) and (exists ( -- ... where the specific column was updated
select null
from deleted d
where d.Id = t.Id
and d.some_column <> t.some_column -- add some form of coalesce here if column is nullable.
)
or not exists ( -- ... or the whole row was inserted.
select null
from deleted d
where d.Id = t.Id
)
);
END
GO
以上作出以下假设。 如果这些都不正确,则必须对代码进行一些调整:
Id
的主键列。 some_column
不能为空。 如果是,请调整条件以解决空值。 recursive triggers enabled
设置设置为false
。 如果您不知道此设置,则可能还可以。 CREATE TRIGGER trig_update
ON <table_name> or <database_name>
Instead of INSERT, UPDATE, DELETE /*made this instead of as I see your saying Before*/
AS
Being
<your sql code>
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.