触发器SQL

CREATE
    TRIGGER FUEL.HDGJcoaldayexpend1_insertTRIGGER AFTER
INSERT
        ON
        FUEL.coaldayexpend1 REFERENCING NEW AS new FOR EACH ROW MODE DB2SQL 
        
        begin atomic 
        
        declare expDate TIMESTAMP ; declare plantCode VARCHAR(32) ;
        declare powerQuan DOUBLE ; declare heatQuan DOUBLE ; declare netQuan DOUBLE ;
        declare beltQuan DOUBLE ; declare beltAdjustQuan DOUBLE ; declare powerExpQuan
        DOUBLE ; declare heatExpQuan DOUBLE ; declare otherExpQuan DOUBLE ; declare
        scheduleQuan DOUBLE ; declare remark VARCHAR(512); declare status VARCHAR(16) ;
        declare operateDate TIMESTAMP ; declare operator VARCHAR(50) ; declare
        expDateChar VARCHAR(32) ; declare operateDateChar VARCHAR(32) ; declare xml_id
        BIGINT;
SET
    ( expDate,
    plantCode,
    powerQuan,
    heatQuan,
    netQuan,
    beltQuan,
    beltAdjustQuan,
    powerExpQuan,
    heatExpQuan,
    otherExpQuan,
    scheduleQuan,
    remark,
    status,
    operateDate,
    operator ) = ( new.expDate,
    new.plantCode,
    new.powerQuan,
    new.heatQuan,
    new.netQuan,
    new.beltQuan,
    new.beltAdjustQuan,
    new.powerExpQuan,
    new.heatExpQuan,
    new.otherExpQuan,
    new.scheduleQuan,
    new.remark,
    new.status,
    new.operateDate,
    new.operator ); 
    if ( expDate IS null ) then
SET
    expDateChar = '' ; else
SET
    expDateChar = char(date(expDate)) ; 
    end if ;
    
     if ( plantCode IS null ) then
SET
    plantCode = '' ; end if ;
    
     if ( remark IS null ) then
SET
    remark = '' ; end if ;
    
     if ( status IS null ) then
SET
    status = '99' ; end if ; 
    
    if ( operateDate IS null ) then
SET
    operateDateChar = '' ; else
SET
    operateDateChar = char(date(operateDate))||' '||char(time(operateDate)); 
    end if ; 
    
    if ( operator IS null ) then
SET
    operator = '' ; end if ; 
    


    if NOT exists(
SELECT
    xml_id
FROM
    fuel.jt_hdgj
WHERE
    tablename = 'coaldayexpend1' AND
    hdgj_id = char(new.id)) then
    
INSERT
    INTO platform.DATA_COALDAYEXPEND1(timemark,
    itemid,
    datadetail)
VALUES
    (char(current date) || ' '||char( current time ),
    plantCode,
    '<CoalDayExpendLiang> <expDate>'||expDateChar||'</expDate> 
    <plantCode>'||plantCode||'</plantCode>
    <powerQuan>'||fuel.doutochar(powerQuan)||'</powerQuan>
    <heatQuan>'||fuel.doutochar(heatQuan)||'</heatQuan>
    <netQuan>'||fuel.doutochar(netQuan)||'</netQuan>
    <beltQuan>'||fuel.doutochar(beltQuan)||'</beltQuan>
    <beltAdjustQuan>'||fuel.doutochar(beltAdjustQuan)||'</beltAdjustQuan>
    <powerExpQuan>'||fuel.doutochar(powerExpQuan)||'</powerExpQuan>
    <heatExpQuan>'||fuel.doutochar(heatExpQuan)||'</heatExpQuan>
    <otherExpQuan>'||fuel.doutochar(otherExpQuan)||'</otherExpQuan>
    <scheduleQuan>'||fuel.doutochar(scheduleQuan)||'</scheduleQuan>
    <status>'||status||'</status> <remark>'||remark||'</remark>
    <operateDate>'||operateDateChar||'</operateDate>
    <operator>'||operator||'</operator> </CoalDayExpendLiang> ');
SET
    (xml_id) = (
SELECT
    identity_val_local()
FROM
    sysibm.sysdummy1) ;
INSERT
    INTO fuel.jt_hdgj(tablename,
    xml_id,
    hdgj_id)
VALUES
    ('coaldayexpend1',
    xml_id,
    to_char(new.id)); end if; 

    
    if(exists(
SELECT
    *
FROM
    FUEL.PICOALPROCESSSTATUS
WHERE
    TABLENAME='COALDAYEXPEND1' AND
    PKID= new.id )) then
UPDATE
    FUEL.PICOALPROCESSSTATUS
SET
    LAST_UPDATE_DATE=new.LAST_UPDATE_DATE ,
    PROCESSTIME=new.LAST_UPDATE_DATE
WHERE
    TABLENAME='COALDAYEXPEND1' AND
    PKID= new.id ; else
INSERT
    INTO FUEL.PICOALPROCESSSTATUS (TABLENAME,
    PKID,
    PROCESS,
    LAST_UPDATE_DATE,
    PROCESSTIME)
VALUES
    ('COALDAYEXPEND1',
    new.id,
    '已处理',
    new.LAST_UPDATE_DATE,
    new.LAST_UPDATE_DATE); end if; END
原文地址:https://www.cnblogs.com/chuanqiMa/p/7003223.html