Home » SQL & PL/SQL » SQL & PL/SQL » Passing table names as parameter in PL/SQL
Passing table names as parameter in PL/SQL [message #35625] Tue, 02 October 2001 06:17 Go to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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.

----------------------------------------------------------------------
Thank you Todd . Re: Passing table names as parameter in PL/SQL [message #35674 is a reply to message #35669] Thu, 04 October 2001 18:36 Go to previous messageGo to next message
Bj
Messages: 8
Registered: March 1999
Junior Member
Thank you Todd

----------------------------------------------------------------------
Re: Passing table names as parameter in PL/SQL [message #125751 is a reply to message #35625] Wed, 29 June 2005 02:09 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #125855 is a reply to message #125808] Wed, 29 June 2005 09:12 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
8i desn't support it.

http://download-uk.oracle.com/docs/cd/A87860_01/doc/index.htm

Quote:


EXECUTE IMMEDIATE dynamic_string
[INTO {define_variable[, define_variable]... | record}]
[USING [IN | OUT | IN OUT] bind_argument
[, [IN | OUT | IN OUT] bind_argument]...]
[{RETURNING | RETURN} INTO bind_argument[, bind_argument]...];



in 9i:

http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96624/11_dynam.htm#16379

Quote:


This statement lets you bulk-bind define variables or OUT bind arguments passed as parameters to a dynamic SQL statement. The syntax follows:

EXECUTE IMMEDIATE dynamic_string
[[BULK COLLECT] INTO define_variable[, define_variable ...]]
[USING bind_argument[, bind_argument ...]]
[{RETURNING | RETURN}
BULK COLLECT INTO bind_argument[, bind_argument ...]];



Rgds.
Re: Passing table names as parameter in PL/SQL [message #128513 is a reply to message #125855] Tue, 19 July 2005 02:09 Go to previous messageGo to next message
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 Go to previous message
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.
Previous Topic: viewing view text
Next Topic: Concatenation QUERY
Goto Forum:
  


Current Time: Sat Dec 07 22:06:52 CST 2024