简体   繁体   English

将 Oracle SQL 触发器转换为 SQL 服务器触发器

[英]Convert Oracle SQL trigger into a SQL Server trigger

I have tried converting trigger written in Oracle to SQL Server triggers.我尝试将用 Oracle 编写的触发器转换为 SQL 服务器触发器。 But it does not allow to use table aliases and ":OLD" keyword in the query.但它不允许在查询中使用表别名和“:OLD”关键字。 Any idea how to do the correct conversion?知道如何进行正确的转换吗?

Oracle trigger: Oracle 触发器:

   CREATE OR REPLACE TRIGGER TRG_DEL_AM_LABELS   BEFORE DELETE on AM_LABELS
    FOR EACH ROW 
       BEGIN
         DELETE FROM AM_LABEL_URLS ALU WHERE ALU.LABEL_ID = :OLD.LABEL_ID;
       END;

Converted SQL Server trigger:转换后的 SQL 服务器触发器:

CREATE OR ALTER TRIGGER TRG_DEL_AM_LABELS 
ON AM_LABELS    
INSTEAD OF DELETE
AS 
BEGIN
    DELETE FROM AM_LABEL_URLS ALU 
    WHERE ALU.LABEL_ID = :OLD.LABEL_ID;
END

CLOSE DELETED_CUR;
DEALLOCATE DELETED_CUR;
DELETE FROM AM_LABELS WHERE (.LABEL_ID;) IN (SELECT .LABEL_ID; FROM DELETED); 
;

These are the errors that popped up.这些是弹出的错误。

Incorrect syntax near ':'. ':' 附近的语法不正确。

Incorrect syntax near 'ALU'. “ALU”附近的语法不正确。

But as it provided in documentation this query is valid to be used in SQL Server.但正如文档中提供的那样,此查询可在 SQL 服务器中使用。

Use the DELETED table to access the old values:使用 DELETED 表访问旧值:

DML trigger statements use two special tables: the deleted table and the inserted tables. DML 触发器语句使用两个特殊表:已删除表和已插入表。 SQL Server automatically creates and manages these tables. SQL 服务器自动创建和管理这些表。 You can use these temporary, memory-resident tables to test the effects of certain data modifications and to set conditions for DML trigger actions.您可以使用这些临时的、驻留在内存中的表来测试某些数据修改的效果并为 DML 触发操作设置条件。

The deleted table stores copies of the affected rows during DELETE and UPDATE statements.删除的表在 DELETE 和 UPDATE 语句期间存储受影响行的副本。 During the execution of a DELETE or UPDATE statement, rows are deleted from the trigger table and transferred to the deleted table.在执行 DELETE 或 UPDATE 语句期间,行会从触发器表中删除并转移到已删除的表中。 The deleted table and the trigger table ordinarily have no rows in common.删除的表和触发器表通常没有共同的行。

See https://docs.microsoft.com/en-us/sql/relational-databases/triggers/use-the-inserted-and-deleted-tables?view=sql-server-ver15请参阅https://docs.microsoft.com/en-us/sql/relational-databases/triggers/use-the-inserted-and-deleted-tables?view=sql-server-ver15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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