将SQL Server触发器转换为oracle触发器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将SQL Server触发器转换为oracle触发器相关的知识,希望对你有一定的参考价值。

我试图将下面显示的SQL Server触发器转换为Oracle触发器,但我得到编译时错误。

SQL Server触发器:

CREATE TRIGGER [dbo].[triggerName]   
ON [dbo].[table] 
AFTER INSERT, UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    IF EXISTS (SELECT * FROM deleted) -- UPDATE
    BEGIN
        IF NOT (UPDATE (lastupdated))  
        BEGIN  
            UPDATE [table] 
            SET lastupdated = GETUTCDATE() 
            WHERE EventKey IN (SELECT EventKey FROM inserted);

            INSERT INTO table2 ([EventKey], [ProgramKey], [ProgramActivityKey],
                                [ParentEventKey], [ComplianceDate],
                                [CycleNumber], [Priority], [WorkPolygonKey],
                                [Shape], [IsCompleted], [IsFieldException],
                                [CompletedDate], [DueDate],
                                [IsSystemException], [FromDate], [ToDate],
                                [CrewKey], [UserKey], [UserId])
                (SELECT
                     e.[EventKey], [ProgramKey], [ProgramActivityKey],
                     [ParentEventKey], [ComplianceDate],
                     [CycleNumber], [Priority], [WorkPolygonKey],
                     [Shape], [IsCompleted], [IsFieldException],
                     [CompletedDate], [DueDate], 
                     [IsSystemException], [lastupdated], GETUTCDATE(),
                     [CrewKey], w.[UserKey], [UserId]
                 FROM
                     deleted e 
                 LEFT OUTER JOIN
                     table3 w ON e.EventKey = w.EventKey 
                 LEFT OUTER JOIN
                     [table4] u ON u.UserKey = w.UserKey);
        END;
    END;
    ELSE 
    BEGIN -- INSERT
        UPDATE [table] 
        SET lastupdated = GETUTCDATE() 
        WHERE EventKey IN (SELECT EventKey FROM inserted);
    END
END

转换后的Oracle触发器:

CREATE OR REPLACE TRIGGER tr_lastModified AFTER INSERT OR UPDATE  ON  Event 
FOR EACH ROW
DECLARE
v_userKey NUMBER;
v_eventKey NUMBER;

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    IF UPDATING THEN--if exists (select * from deleted) -- UPDATE  
            IF NOT UPDATING ('lastupdated') THEN

        UPDATE Event SET lastupdated = SYS_EXTRACT_UTC(SYSTIMESTAMP) WHERE EventKey = :NEW.EventKey;

        SELECT userkey INTO v_userKey FROM WorkAssignment w WHERE w.EventKey = :OLD.EventKey LEFT JOIN User_ u ON u.UserKey = w.UserKey;

            INSERT INTO EventHistory VALUES (:OLD.EventKey,
                                                    :OLD.ProgramKey,
                                                    :OLD.ProgramActivityKey,
                                                    :OLD.ParentEventKey,
                                                    :OLD.ComplianceDate,
                                                    :OLD.CycleNumber,
                                                    :OLD.Priority,
                                                    :OLD.WorkPolygonKey,
                                                    :OLD.Shape,
                                                    :OLD.IsCompleted,
                                                    :OLD.IsFieldException,
                                                    :OLD.CompletedDate,
                                                    :OLD.DueDate,
                                                    :OLD.IsSystemException,
                                                    :OLD.FromDate,
                                                    SYS_EXTRACT_UTC(SYSTIMESTAMP),
                                                    :OLD.CrewKey,
                                                    v_userKey,
                                                    :OLD.UserId);           
      END IF;
    ELSE
            UPDATE Event SET lastupdated = SYS_EXTRACT_UTC(SYSTIMESTAMP) WHERE  EventKey = :NEW.EventKey;
  END IF;    
END;
答案

此SELECT中的语法无效;我修好了它:

 SELECT userkey
   INTO v_userKey
   FROM WorkAssignment w LEFT JOIN User_ u ON u.UserKey = w.UserKey
  WHERE w.EventKey = :OLD.EventKey;

此外,似乎你在任何情况下都在执行UPDATE Event,所以 - 也许你应该将它移出IF-THEN-ELSE

以上是关于将SQL Server触发器转换为oracle触发器的主要内容,如果未能解决你的问题,请参考以下文章

如何将 DB2 触发器转换为 SQL Server?

MySQL 触发器中的“INSTEAD OF”,从 SQL Server 转换而来

SQL Server 触发 CAST 函数

触发器实现对插入数据的字段更改 Oracle+SQL Server

SQL server与Oracle触发器的创建与使用

将 Oracle 触发器转换为 MySql 触发器