触发器SQL

Posted AmourOnce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了触发器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

 

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

(十七)PL/SQL DBMS输出

如何设置 vscode 的代码片段,以便在自动完成后自动触发 vscode 的智能感知?

使用触发器对学生表操作进行日志记录

SQL记录-PLSQL-DBMS输出

片段创建的 Intent 不会触发 onNewIntent

sql sql里面的代码片段