Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple SQL statements in FORALL loop

I want to insert in Different tables with only single FORALL Loop in oracle.but FORALL don't support it.any idea how can i do it??

   create or replace PROCEDURE test IS
      TYPE avl_web_details IS TABLE OF available_web_details%ROWTYPE;
      var_avl_web_details avl_web_details := avl_web_details();
      UNIONTABLE VARCHAR2(30000);
      TYPE RepCurTyp IS REF CURSOR; 
      Rep_cv RepCurTyp; 
   BEGIN
      UNIONTABLE := '';
  execute immediate 'update tbl_used_webuda1 set flag=1';
  FOR tbl IN (SELECT tablename FROM tbl_used_webuda1 where flag=1)
      LOOP             
    UNIONTABLE := UNIONTABLE || 'select *' || ' from ' || tbl.tablename || 'union all ';     
END LOOP;

       IF (LENGTH(UNIONTABLE) > 10) THEN
      UNIONTABLE := '( ' || SUBSTR(UNIONTABLE,1, length(UNIONTABLE)-10) || ' ) ';            
     end if;
      OPEN Rep_cv FOR 'select from_unixtime("5mintime") as "5mintime",username,host,src_zone,domain,dst_zone,content,category,url,hits ,bytes,appid ,application,categorytype,usergroup from'|| uniontable;
LOOP 
    FETCH Rep_cv BULK COLLECT INTO var_avl_web_details LIMIT 200000;
        FORALL i IN 1..var_avl_web_details.COUNT
            insert into available_web_details values var_avl_web_details(i);
    insert into web_ap_ca_co_do_ur_us_5min values(ts ,var_avl_web_details(i).application ,var_avl_web_details(i).category ,var_avl_web_details(i).content ,var_avl_web_details(i).domain ,var_avl_web_details(i).dst_zone ,var_avl_web_details(i).url ,var_avl_web_details(i).username ,var_avl_web_details(i).hits,var_avl_web_details(i).bytes,var_avl_web_details(i).appid);     
    insert into web_user_5min values(ts,var_avl_web_details(i).username,var_avl_web_details(i).hits,var_avl_web_details(i).bytes,var_avl_web_details(i).appid);
    EXIT WHEN Rep_cv%NOTFOUND; 
end loop; 
close rep_cv; 
end;**
like image 273
Asha Koshti Avatar asked Mar 14 '12 13:03

Asha Koshti


People also ask

What is the difference between bulk collect and forall?

BULK COLLECT: These are SELECT statements that retrieve multiple rows with a single fetch, thereby improving the speed of data retrieval. FORALL: These are INSERT, UPDATE, and DELETE operations that use collections to change multiple rows of data very quickly.

Is bulk collect faster than cursor?

Definitely there is increase in performance with bulk insert because of less number of context switches from PLSQL engine to SQL engine when compared to cursor for loop insert.

Which command Cannot be used with forall command?

The following restrictions apply to the FORALL statement: You cannot loop through the elements of an associative array that has a string type for the key. Within a FORALL loop, you cannot refer to the same collection in both the SET clause and the WHERE clause of an UPDATE statement.

Can we use bulk collect in cursor?

This BULK COLLECT can be used in 'SELECT' statement to populate the records in bulk or in fetching the cursor in bulk. Since the BULK COLLECT fetches the record in BULK, the INTO clause should always contain a collection type variable.


2 Answers

FORALL is not a "loop command", it is part of the syntax for a bulk insert statement. So the correct solution is to write the FORALL clause with each insert:

FORALL i IN 1..var_avl_web_details.COUNT
    insert into available_web_details values var_avl_web_details(i);

FORALL i IN 1..var_avl_web_details.COUNT 
    insert into web_ap_ca_co_do_ur_us_5min values(ts ,var_avl_web_details(i).application ,var_avl_web_details(i).category ,var_avl_web_details(i).content ,var_avl_web_details(i).domain ,var_avl_web_details(i).dst_zone ,var_avl_web_details(i).url ,var_avl_web_details(i).username ,var_avl_web_details(i).hits,var_avl_web_details(i).bytes,var_avl_web_details(i).appid);      

FORALL i IN 1..var_avl_web_details.COUNT
    insert into web_user_5min values(ts,var_avl_web_details(i).username,var_avl_web_details(i).hits,var_avl_web_details(i).bytes,var_avl_web_details(i).appid);
like image 152
Tony Andrews Avatar answered Sep 29 '22 07:09

Tony Andrews


No need to call FORALL many times. Just use INSERT ALL DML statement.


forall i in v_list.first..v_list.last
  insert all
    into t116 (id) values (v_list(i))
    into t117 (id) values (v_list(i))
      select 1 from dual;
like image 45
niteshade Avatar answered Sep 29 '22 07:09

niteshade