ORACLE PL / SQL:使用多个参数调用存储过程函数(DML查询)

时间:2017-03-10 17:56:05

标签: oracle stored-procedures plsql stored-functions

这里有新的PL / SQL人员。我有一个(成功编译的)PL / SQL功能块,通过向其中添加一个新术语来操作数据库中的表:

create or replace FUNCTION add_new_term
    (TERM_ID_IN IN NUMBER, TERM_IN IN VARCHAR2, IS_METATERM_IN IN NUMBER)
    RETURN VARCHAR2
IS
    add_term CV_TERMS.TERM_NAME%TYPE; --TERM_NAME is VARCHAR2 type
BEGIN
    INSERT INTO CV_TERMS (TERM_ID, TERM_NAME, IS_METATERM)
    VALUES (TERM_ID_IN, TERM_IN, IS_METATERM_IN);
    dbms_output.put_line('New term successfully added to CV_TERMS table: ' || TERM_IN);
    RETURN add_term;
EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
        raise_application_error (-20001, 'You have tried to insert a duplicate term.');
    WHEN OTHERS THEN    
        raise_application_error (-20002, 'An error has occurred inserting a term - '|| SQLCODE ||' -ERROR- '|| SQLERRM);
END add_new_term;

我将此函数称为调用存储过程:

DECLARE
  add_term_success cv_terms.term_name%type;
BEGIN
  add_term_success := add_new_term(cv_terms_pk.NEXTVAL, 'TESTTT', 0);
END;

SQLDeveloper告诉我程序已成功完成,但该术语尚未添加到表中。我独立创建了序列cv_terms_pk(它不在表CV_TERMS' SQL中)。它需要在那里吗?我不正确地传递它吗?或者我的add_term声明有问题吗?想法?

1 个答案:

答案 0 :(得分:1)

在DML INSERT之后,您必须提交事务。

create or replace FUNCTION add_new_term
(TERM_ID_IN IN NUMBER, TERM_IN IN VARCHAR2, IS_METATERM_IN IN NUMBER)
RETURN VARCHAR2 IS
add_term CV_TERMS.TERM_NAME%TYPE; --TERM_NAME is VARCHAR2 type

BEGIN INSERT INTO CV_TERMS(TERM_ID, TERM_NAME, IS_METATERM VALUES (TERM_ID_IN, TERM_IN, IS_METATERM_IN); COMMIT; ---LINE ADDED ...