简体   繁体   中英

How to convert DB2 trigger to SQL Server?

I am converting some DB2 triggers to SQL Server. One of them shown here I am struggling with it, Conversion tried at certain extend but seems not good enough to work out.

Trigger execute before an update or insert and selecting the value of column PDE_KEY based on Select statement in brackets with where clause before written to the database.

Need help.

DB2 trigger

CREATE OR REPLACE TRIGGER "TDC" BEFORE
INSERT
    ON
    "DECL_CONSIGNMENT" REFERENCING NEW AS NEW FOR EACH ROW NOT SECURED SET
    NEW.PDE_KEY = (
    SELECT
        D.PDE_KEY
    FROM
        DECL D
    WHERE
        D.DE_KEY = NEW.DE_KEY)

SQL Server conversion of this trigger (my attempt):

CREATE TRIGGER [TDC]
ON [DECL_CONSIGNMENT]
INSTEAD OF INSERT
AS 
BEGIN
    DECLARE @PDE_KEY BIGINT
    SET NOCOUNT ON

    SELECT
        @PDE_KEY = D.PDE_KEY
    FROM
        DECL D
    WHERE
        D.DE_KEY = (...work upto here)
END

An instead of trigger must explicitly execute the dml needed.

REATE TRIGGER [TDC]
ON [DECL_CONSIGNMENT]
INSTEAD OF INSERT
AS 
BEGIN       
    SET NOCOUNT ON;
    INSERT INTO [DECL_CONSIGNMENT] (a, b, .. PDE_KEY, ..)
    SELECT a, b, .. (
       SELECT
         D.PDE_KEY
       FROM
         DECL D
       WHERE
         D.DE_KEY = i.DE_KEY),
       ..
    FROM inserted i;
END

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.

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