oracle PL/SQL 调用触发器内的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18158894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:51:35  来源:igfitidea点击:

PL/SQL calling a function inside a trigger

oraclefunctionplsqltriggers

提问by teppuus

I am trying to create a function using Oracle PL/SQL that stores the current date into a variable. This function will be called inside a trigger. The trigger (among other things) will insert this variable into a new table that has already been created. My code complies and runs without errors, but it doesn't work. Nothing happens. The trigger is an ON DELETE trigger, so when I delete one row from the original table, it just stays. Any clues what I am missing? Thank you.

我正在尝试使用 Oracle PL/SQL 创建一个将当前日期存储到变量中的函数。此函数将在触发器内调用。触发器(除其他外)会将此变量插入到已创建的新表中。我的代码符合并且运行没有错误,但它不起作用。没发生什么事。触发器是一个 ON DELETE 触发器,所以当我从原始表中删除一行时,它只是保持不变。我缺少什么线索?谢谢你。

Function:

功能:

    CREATE OR REPLACE FUNCTION get_date (i_stdid archive_student.stdid%TYPE)
    RETURN date
    AS
       v_date DATE;
    BEGIN
       SELECT CURRENT_DATE INTO v_date FROM DUAL; 
    RETURN v_date;
    END;

Function call inside trigger:

触发器内部的函数调用:

    CREATE OR REPLACE TRIGGER ARCHIVE_DELETED_STUDENT
    AFTER DELETE ON STUDENT
    FOR EACH ROW
    DECLARE 
        v_date archive_student.change_date%TYPE;
    BEGIN
        -- other if statements here that are working properly
        v_date := get_date(:old.stdid);
        INSERT INTO archive_student (change_date) VALUES (v_date);
    END;

采纳答案by ThinkJet

You doing well, check this SQLFiddle.

你做得很好,检查这个 SQLFiddle

Only one thing - you missed stdidwhile inserting into archive_studentso it is null after insert.

只有一件事 - 你stdid在插入时错过了archive_student所以插入后它是空的。