Friday, March 4, 2011

Dynamic SQL - Execute Immediate -1

CREATE OR REPLACE PROCEDURE raise_emp_sal
       (v_emp_column_name VARCHAR2,
        v_column_value NUMBER, v_amount NUMBER) IS

  l_sql_stmt VARCHAR2(2000);
BEGIN
  
  l_sql_stmt :='UPDATE emp_t SET sal = sal + :1'||
               'WHERE '|| v_emp_column_name || '= :3';
  EXECUTE IMMEDIATE l_sql_stmt USING v_amount,v_column_value;

  IF SQL%ROWCOUNT > 0 THEN
     DBMS_OUTPUT.PUT_LINE('Salary has been updated for '
              ||v_emp_column_name || ' = '||v_column_value||
              ' by '||v_amount||' Rupees');
  END IF;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Invalid column name '
                         ||v_emp_column_name);
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Others exception raised '|| sqlerrm);
END;


SQL> SET SERVEROUTPUT ON
SQL>
SQL> BEGIN
  2    RAISE_EMP_SAL('EMPNO',7654,1200);
  3    RAISE_EMP_SAL('DEPTNO',20,100);
  4    COMMIT;
  5  END;
  6  /

Salary has been updated for EMPNO = 7654 by 1200 Rupees
Salary has been updated for DEPTNO = 20 by 100 Rupees

PL/SQL procedure successfully completed

No comments:

Post a Comment

External Table

Oracle External Table External tables are defined as tables that do not resides in the database allows you to access data that is stor...