【问题标题】:Trigger From Oracle to SQL SERVER从 Oracle 触发到 SQL SERVER
【发布时间】:2015-08-24 18:39:45
【问题描述】:

您好,我怎样才能将此触发器从 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;

感谢您的帮助。

【问题讨论】:

  • 我们不是代码翻译服务。

标签: sql sql-server oracle11g


【解决方案1】:

在这种情况下,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。如果您不了解此设置,那么您可能没问题。

【讨论】:

    【解决方案2】:
    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;
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2011-09-11
      • 1970-01-01
      • 2019-04-08
      • 2010-09-07
      • 2020-05-09
      相关资源
      最近更新 更多