Accessing a table from a name in a variable
715700Aug 28 2009 — edited Aug 28 2009Hi there
So here's what I'm trying to do.
I have a software that creates tables with this nomenclatur : XB[TYPE][laste two digit of the year][month][number]
so I got tables like XB0609080002 for the type 6, year 2009, august, number 2.
Now, I'd like to select datas in some of those tables and insert them into another table. For ex, I want to put rows A and B of all the tables of type 4 and 6 of august in a table, so I have to select datas from tables going from XB0409080001 to XB0409080054 and from XB0609080001 to XB0609080031.
I'd like to make a procedure to union all those tables so I can select the datas I want or create a view out of it, or create a procedure to go through the table and copy the relevant data into a single table.
I'm usually not using procedure, so the one I've done isn't working (I'm not even sure I'm invoking the procedure correctly).
Any clues ?
here's the code of my procedure, I've put the month and year parameter hard coded for now:
create or replace Procedure type_paiement
(
journal in varchar2)
is
BEGIN
declare
prefix_ntable varchar2(8);
ntable varchar2(12);
i integer;
ex integer;
mois varchar2(2);
an varchar2(2);
requete varchar2(512);
begin
prefix_ntable:='XB' ||journal ||'09' ||'07';
mois :='07';
an :='09';
for i in 1..100
loop
ntable :=prefix_ntable||lpad ( i,4,'0' ) ;
requete:='select count(*) from user_tables where table_name='''||ntable||'''';
execute immediate requete into ex;
if ex >0 then
requete:='insert into xx_jk_mreg (clie_code,journal,periode) select distinct c.code,
xb.doss,
'||journal||',
'||mois||an||'
from
'||ntable||' xb,
dossier d,
client c
where xb.cmpt=''4111''
and xb.doss =d.code
and c.code =d.clie';
execute immediate requete;
end if;
end loop;
END;
end;
PS: I'm on oracle 9i using SQL developer
Edited by: user5880555 on Aug 28, 2009 9:44 AM