Dec 15, 2009

RETURNING INTO Clause in Oracle

, Nice new feature introduced in Oracle 10g:
Single-Set Aggregates in DML Returning Clause: -as per Oracle documentation- This feature enables the use of single-set aggregation functions in the RETURNING clause of DML statements. This capability can result in significant performance gains in transactions that process many rows of the same table, a behavior often observed in batch processes.
Simple syntax is:

INSERT INTO {table} (columnn) 
{values}
RETURNING {exp} INTO {variables};

where:
can be table, materialized view or single-table view.

The purpose of the RETURNING clause is to return the rows affected by the INSERT statement.

SET serveroutput on

DECLARE
   v_total_salary   NUMBER;
BEGIN
   INSERT INTO emp
      SELECT *
        FROM emp
     RETURNING SUM (sal)
          INTO v_total_salary;

   DBMS_OUTPUT.put_line ('Total Salary is: ' || v_total_salary);
END;
/

RETURNING clause can be used with UPDATE statement:

SET serveroutput on

DECLARE
   v_total_salary   NUMBER;
BEGIN
   UPDATE    emp
         SET sal = sal * 1.1
   RETURNING SUM (sal)
        INTO v_total_salary;

   DBMS_OUTPUT.put_line ('Total Salary is: ' || v_total_salary);
END;
/

RETURNING clause can be used with UPDATE statement also:

SET serveroutput on

DECLARE
   v_total_salary   NUMBER;
BEGIN
   DELETE FROM emp
         WHERE manager IS NOT NULL
     RETURNING SUM (al)
          INTO v_total_salary;

   DBMS_OUTPUT.put_line ('Total Salary is: ');
END;
/

Saad,

No comments:

Post a Comment