Passing table names as parameter in PL/SQL [message #35625] |
Tue, 02 October 2001 06:17 |
Gloria Zhao
Messages: 1 Registered: October 2001
|
Junior Member |
|
|
Hi there,
I need to pass two table names as parameters to a PL/SQL program. One of the table name will be used in a cursor. I would appreciate it if you could please provide a solution for me.
Thanks so much in advance.
Gloria
----------------------------------------------------------------------
|
|
|
Re: Passing table names as parameter in PL/SQL [message #35630 is a reply to message #35625] |
Tue, 02 October 2001 08:14 |
Todd Barry
Messages: 4819 Registered: August 2001
|
Senior Member |
|
|
Here's a one parameter example:
create or replace procedure test(p_table_name in user_tables.table_name%type)
is
type rc is ref cursor;
c rc;
r date;
begin
open c for 'select sysdate from ' || p_table_name;
fetch c into r;
dbms_output.put_line(r);
close c;
end;
/
user@db.world> exec test('dual');
02-OCT-01
PL/SQL procedure successfully completed.
----------------------------------------------------------------------
|
|
|
Re: Passing table names as parameter in PL/SQL [message #35669 is a reply to message #35630] |
Thu, 04 October 2001 15:47 |
Bj
Messages: 8 Registered: March 1999
|
Junior Member |
|
|
I'm using 8.0.6
Tried this didn't work
errors at line open c for 'select sysdate...
dosen't seems to like select in quotes
Please let me know if I'm off track
Cheers
----------------------------------------------------------------------
|
|
|
Re: Passing table names as parameter in PL/SQL [message #35672 is a reply to message #35630] |
Thu, 04 October 2001 17:49 |
Todd Barry
Messages: 4819 Registered: August 2001
|
Senior Member |
|
|
No, sorry, this won't work in 8.0x. Unless someone specifically mentions otherwise, I am assuming they are running on an 8i or later version since it has been out for over two years now.
You'll need to work with DBMS_SQL instead.
----------------------------------------------------------------------
|
|
|
|
Re: Passing table names as parameter in PL/SQL [message #125751 is a reply to message #35625] |
Wed, 29 June 2005 02:09 |
munaga
Messages: 21 Registered: July 2004
|
Junior Member |
|
|
I would like to pass table name as the parameter in the following procedure. It is not working. can you help me.
create or replace procedure test(table_name in user_tables.table_name%type) is
type emptabtype is table of emp%ROWTYPE;
emptab emptabtype;
begin
EXECUTE IMMEDIATE 'SELECT * BULK COLLECT INTO emptab FROM' ||table_name ;
FOR i IN emptab.FIRST..emptab.last
LOOP
dbms_output.put_line('Employee name = '||emptab(i).ename);
END LOOP;
end;
/
|
|
|
Re: Passing table names as parameter in PL/SQL [message #125756 is a reply to message #125751] |
Wed, 29 June 2005 02:22 |
dmitry.nikiforov
Messages: 723 Registered: March 2005
|
Senior Member |
|
|
EXECUTE IMMEDIATE has another syntax for BULK COLLECT:
SQL> create or replace procedure
2 n_test(table_name in user_tables.table_name%type) is
3 type emptabtype is table of emp%ROWTYPE;
4 emptab emptabtype;
5 begin
6 EXECUTE IMMEDIATE 'SELECT * FROM ' || table_name
7 BULK COLLECT INTO emptab;
8 FOR i IN emptab.FIRST..emptab.last
9 LOOP
10 dbms_output.put_line('Employee name = ' || emptab(i).ename);
11 END LOOP;
12 end;
13 /
Procedure created.
Elapsed: 00:00:00.40
SQL> exec n_test('emp');
Employee name = SMITH
Employee name = ALLEN
Employee name = WARD
Employee name = JONES
Employee name = MARTIN
Employee name = BLAKE
Employee name = CLARK
Employee name = SCOTT
Employee name = KING
Employee name = TURNER
Employee name = ADAMS
Employee name = JAMES
Employee name = FORD
Employee name = MILLER
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.06
Rgds.
|
|
|
Re: Passing table names as parameter in PL/SQL [message #125808 is a reply to message #125756] |
Wed, 29 June 2005 05:33 |
resy
Messages: 86 Registered: December 2003
|
Member |
|
|
Is that working?
Im getting the following error
7/7 PLS-00103: Encountered the symbol "BULK" when expecting one of
the following:
. ( * @ % & = - + ; < / > at in mod not rem return returning
<an exponent (**)> <> or != or ~= >= <= <> and or like
between into using is null is not || is dangling
Im using Oracle 8i
|
|
|
|
Re: Passing table names as parameter in PL/SQL [message #128513 is a reply to message #125855] |
Tue, 19 July 2005 02:09 |
bose_gvc
Messages: 1 Registered: July 2005 Location: Hyd
|
Junior Member |
|
|
Hai,
I have to pass a part of a table name to select,insert and update queries
for example cdrs_del is the table name
then i want to pass the parameter as del
and i want to concatinate it with the query like
select * from cdrs_||parameter
please advice
|
|
|
Re: Passing table names as parameter in PL/SQL [message #128520 is a reply to message #128513] |
Tue, 19 July 2005 02:36 |
dmitry.nikiforov
Messages: 723 Registered: March 2005
|
Senior Member |
|
|
See OPEN FOR, EXECUTE IMMEDIATE and DBMS_SQL in Oracle
docs.
SQL> declare
2 tabname varchar2(10) := 'emp';
3 rc sys_refcursor;
4 ename emp.ename%type;
5 begin
6 open rc for 'select ename from ' || tabname;
7 end;
8 /
PL/SQL procedure successfully completed.
SQL> declare
2 tabname varchar2(10) := 'emp';
3 begin
4 execute immediate 'update ' || tabname || ' set ename = upper(ename)';
5 end;
6 /
PL/SQL procedure successfully completed.
Rgds.
|
|
|