DECLARE
TYPE emp_ref_cursor IS REF CURSOR;
TYPE empno_tab IS TABLE OF NUMBER;
TYPE ename_tab IS TABLE OF VARCHAR2(50);
v_emp_cur emp_ref_cursor;
vc_empno empno_tab;
vc_sal empno_tab;
vc_ename ename_tab;
vc_ename1 ename_tab;
vc_ename2 ename_tab;
v_sql_stmt VARCHAR2(500);
v_bonus CONSTANT NUMBER := 500;
a_null CHAR(1);
BEGIN
-- example of ref cursor with bulk collect
v_sql_stmt := 'SELECT empno, ename FROM emp_t WHERE deptno = 10';
OPEN v_emp_cur FOR v_sql_stmt;
FETCH v_emp_cur BULK COLLECT INTO vc_empno,vc_ename;
CLOSE v_emp_cur;
-- example of dynamic sql with bull collect
v_sql_stmt := 'SELECT sal FROM emp_t';
EXECUTE IMMEDIATE v_sql_stmt BULK COLLECT INTO vc_sal;
-- example of dynamic sql with returning bulk collect
v_sql_stmt := 'UPDATE emp_t SET sal = sal + :1
RETURNING ename INTO :2';
EXECUTE IMMEDIATE v_sql_stmt
USING v_bonus RETURNING BULK COLLECT INTO vc_ename1;
--dynamic sql inside forall statement
v_sql_stmt := 'UPDATE emp_t SET sal = sal - :1
WHERE empno = :2 RETURNING ENAME INTO :3';
FORALL i in vc_empno.first..vc_empno.last
EXECUTE IMMEDIATE v_sql_stmt USING v_bonus, vc_empno(i)
RETURNING BULK COLLECT INTO vc_ename2;
FOR i in vc_ename2.first..vc_ename2.last LOOP
dbms_output.put_line(vc_ename2(i));
END LOOP;
-- passing null to dynamic sql
EXECUTE IMMEDIATE 'UPDATE emp_t SET comm = :1' USING a_null;
ROLLBACK;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('When others exception raised '|| sqlerrm);
END;
TYPE emp_ref_cursor IS REF CURSOR;
TYPE empno_tab IS TABLE OF NUMBER;
TYPE ename_tab IS TABLE OF VARCHAR2(50);
v_emp_cur emp_ref_cursor;
vc_empno empno_tab;
vc_sal empno_tab;
vc_ename ename_tab;
vc_ename1 ename_tab;
vc_ename2 ename_tab;
v_sql_stmt VARCHAR2(500);
v_bonus CONSTANT NUMBER := 500;
a_null CHAR(1);
BEGIN
-- example of ref cursor with bulk collect
v_sql_stmt := 'SELECT empno, ename FROM emp_t WHERE deptno = 10';
OPEN v_emp_cur FOR v_sql_stmt;
FETCH v_emp_cur BULK COLLECT INTO vc_empno,vc_ename;
CLOSE v_emp_cur;
-- example of dynamic sql with bull collect
v_sql_stmt := 'SELECT sal FROM emp_t';
EXECUTE IMMEDIATE v_sql_stmt BULK COLLECT INTO vc_sal;
-- example of dynamic sql with returning bulk collect
v_sql_stmt := 'UPDATE emp_t SET sal = sal + :1
RETURNING ename INTO :2';
EXECUTE IMMEDIATE v_sql_stmt
USING v_bonus RETURNING BULK COLLECT INTO vc_ename1;
--dynamic sql inside forall statement
v_sql_stmt := 'UPDATE emp_t SET sal = sal - :1
WHERE empno = :2 RETURNING ENAME INTO :3';
FORALL i in vc_empno.first..vc_empno.last
EXECUTE IMMEDIATE v_sql_stmt USING v_bonus, vc_empno(i)
RETURNING BULK COLLECT INTO vc_ename2;
FOR i in vc_ename2.first..vc_ename2.last LOOP
dbms_output.put_line(vc_ename2(i));
END LOOP;
-- passing null to dynamic sql
EXECUTE IMMEDIATE 'UPDATE emp_t SET comm = :1' USING a_null;
ROLLBACK;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('When others exception raised '|| sqlerrm);
END;
No comments:
Post a Comment