0

New PL/SQL person here. I have a (successfully compiled) PL/SQL function block that manipulates a table in my database by adding a new term to it:

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;

I call this function like calling a stored procedure:

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

SQLDeveloper tells me the procedure was successfully completed, however, the term has not been added to the table. I created the sequence cv_terms_pk independently (it's not in the table CV_TERMS' SQL). Does it need to be there? Am I passing it improperly? Or is something wrong with my add_term declaration? Ideas?

6
  • 3
    do you commit somewhere?
    – tbone
    Commented Mar 10, 2017 at 18:34
  • Did you query the table to verify it isn't there? I noticed that you are returning add_term, but never setting a value. Likewise, you are returning a line of output, but after you have already ceased execution due to the return. So your return value will always be NULL and no output will ever show up in the console.
    – Chris Hep
    Commented Mar 10, 2017 at 19:07
  • @HepC I queried the table, indeed. They are writing to the table. Silly me. When I searched for them before within the table, they weren't showing up. So everything works except the console output. Could you explain a little more? I understand that I put the return line after the return (I switched it), but as far as setting a value to add_term? @tbone I did not commit.
    – snl330
    Commented Mar 10, 2017 at 20:46
  • 1
    For getting a value into your return parameter, you just need to use the RETURNING INTO clause, added to the end of your INSERT statement.
    – Chris Hep
    Commented Mar 10, 2017 at 20:58
  • @HepC Got it, thanks. I would +1 you, but I'm not there yet.
    – snl330
    Commented Mar 10, 2017 at 21:25

1 Answer 1

1

After the DML INSERT you have to commit the transaction.

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 ...

1
  • Thanks, I did this already :). Part of the issue was that my console wasn't returning any lines, so I had to SET SERVEROUTPUT ON to get diagnostics. Marked as correct.
    – snl330
    Commented Mar 13, 2017 at 15:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.