Below is the anonymous block:
DECLARE
v_tablename VARCHAR2(20);
sql_statement VARCHAR2(500);
v_empid emp.emp_id%TYPE := 11;
v_empname emp.emp_name%TYPE;
v_deptid emp.dept_id%TYPE;
v_age emp.age%TYPE;
v_sex emp.sex%TYPE;
BEGIN
v_tablename := '&table';
sql_statement := 'UPDATE ' || v_tablename || ' SET age = age + 1 WHERE emp_id = :1 RETURNING empname,deptid,age,sex INTO :2, :3, :4, :5';
EXECUTE IMMEDIATE sql_statement USING v_empid RETURNING INTO v_empname, v_deptid, v_age, v_sex;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_empname);
DBMS_OUTPUT.PUT_LINE('Dept Id: ' || v_deptid);
DBMS_OUTPUT.PUT_LINE('Age: ' || v_age);
DBMS_OUTPUT.PUT_LINE('Sex: ' || v_sex);
END;
When I execute this, I am getting the below error message:
ORA-00904: "DEPTID": invalid identifier
The error is thrown in the below line:
sql_statement := 'UPDATE ' || v_tablename || ' SET age = age + 1 WHERE emp_id = :1 RETURNING empname,deptid,age,sex INTO :2, :3, :4, :5';
The error sounds like the variable is not already declared but for RETURNING in a DML (i.e. UPDATE in my case) do we really need to declare the variables?
Kindly let me know how to resolve this and make this piece of block work.