Home » SQL & PL/SQL » SQL & PL/SQL » Bulk collect data problem (Oracle 10g)
Bulk collect data problem [message #650104] Fri, 15 April 2016 04:54 Go to next message
cjcsegers
Messages: 1
Registered: April 2016
Location: Netherlands
Junior Member
Hi,

I am just starting my way though the world of dynamic sql's.
So far my first problem is that I need to bulk collect data between different schema's and store all the collected data in a sepearte table created in the main schema.

My first setup does not workout at all.
Anybody who can help me out?

See my setup below Confused
declare

v_proj_name project.proj_name%TYPE;

type mfr_mod_CurTyp IS REF CURSOR;
type type_rec is record
  ( global_id NUMBER(22),
    cmpnt_mfr_name NVARCHAR2(20),
    cmpnt_mod_name NVARCHAR2(100),
    proj_name NVARCHAR2(50),
    proj_id NUMBER(22)
  );
type JNL_mfr_mod_overview is table of type_rec;
mfr_mod mfr_mod_CurTyp;
mfr_mod_ids JNL_mfr_mod_overview;

cursor domains is
    select
      proj_name
    from
      project
    where
      proj_del = 'N'
      --and proj_name <> 'SPARE_PARTS'
       and proj_name = 'asset_gr_spare'     
      and proj_id > 0;

begin
open domains;

execute immediate 'truncate table  amr.JNL_mfr_mod_overview';

loop
  fetch domains into v_proj_name;
  exit when domains%NOTFOUND;
  
OPEN mfr_mod FOR 'select cm.cmpnt_mfr_name, cmd.cmpnt_mod_name from ' || to_char(v_proj_name)  || '.component_mfr cm join component_mod cmd on cm.cmpnt_mfr_id = cmd.cmpnt_mfr_id where cmpnt_mfr_id <> 0';
FETCH mfr_mod BULK COLLECT INTO mfr_mod_ids;
CLOSE mfr_mod;
  execute immediate 
  'select cm.cmpnt_mfr_name, cmd.cmpnt_mod_name from ' || to_char(v_proj_name)  || '.component_mfr cm join component_mod cmd on cm.cmpnt_mfr_id = cmd.cmpnt_mfr_id
  where cmpnt_mfr_id <> 0'
BULK COLLECT INTO JNL_mfr_mod_overview;
end loop;
close domains;
end;


Kind regards,
Eric
*BlackSwan added {code} tags. Please do so yourself in the future.

[Updated on: Fri, 15 April 2016 08:24] by Moderator

Report message to a moderator

Re: Bulk collect data problem [message #650106 is a reply to message #650104] Fri, 15 April 2016 05:39 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Welcome to the forum.
Please read and follow How to use [code] tags and make your code easier to read?

It's always difficult to diagnose problems with code when you don't know how it's failing. Are you getting an error? wrong results?

The code on it's face seems pointless - you're selecting data and doing nothing with it. Seems like an insert is needed and you don't have one.

Also bulk collect isn't nearly as efficient as insert/select so I'd stop trying to use it and just write an insert/select instead.
Re: Bulk collect data problem [message #650107 is a reply to message #650106] Fri, 15 April 2016 05:42 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And finally I'd change the data model, if that's at all feasible, so that you don't have data scattered over multiple schemas. There's no real upside to that approach and you end up using masses of dynamic sql, which tends to be bug prone and cause performance issues.
Re: Bulk collect data problem [message #650108 is a reply to message #650107] Fri, 15 April 2016 05:44 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And why are you using a ref cursor and execute immediate for the same query?
Re: Bulk collect data problem [message #650111 is a reply to message #650108] Fri, 15 April 2016 07:24 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
never do in PL/SQL that which can be done in plain SQL.

how will you & I know when correct solution has been posted here?
Re: Bulk collect data problem [message #650178 is a reply to message #650111] Mon, 18 April 2016 12:52 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
If all you want to do is store a local copy of data on another database server, use a Materilized View. That is exactly what they are designed for and oracle will automatically maintain the table. Also, looking at your select it would be simple to create an MVIEW LOG on the base tables and the MVIEW would only update the changed rows. Works great
Previous Topic: random numbers
Next Topic: query to right outer join and sum up
Goto Forum:
  


Current Time: Wed Apr 24 14:55:06 CDT 2024