Solved

Passing table name into parameter for stored procedure

Posted on 2011-02-24
8
825 Views
Last Modified: 2012-05-11
Hello,

I'm trying to create a procedure with a parameter for a table name. Please help... I though I could write this ref cursor procedure just Like I do for the others.

Thanks
create or replace
PROCEDURE                                           ALL_TBLES_PRC

/* NAME: TBLES   DESCRIPTION: 
      -- This is to select ALL data from Project tables. Parameters will be the TABLE NAME and PUB_ID
   
*/ 
(
  PARAM_0 IN NUMBER, --USER
  PARAM_SYSFLAG IN NUMBER, --NUMBER  PARAM_1 IN VARCHAR2, -- TABLE NAME
  PARAM_2 IN NUMBER, -- PUB ID
  P_RECORDSET OUT SYS_REFCURSOR
) 
AS 
BEGIN
  OPEN P_RECORDSET FOR
    SELECT * FROM 'AAD.'|| PARAM_1
    WHERE ID = PARAM_2;

  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      NULL;
END ALL_TBLES_PRC;

/*

*/

Open in new window

0
Comment
Question by:lulubell-b
  • 6
  • 2
8 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 34973201
OPEN P_RECORDSET FOR
    'SELECT * FROM AAD.'|| PARAM_1 || ' WHERE ID = PARAM_2';

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34973204
when using dynamic sql,  the entire statement must be contained in the string
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 34973212
oops, just noticed the param2 part

make that a bind

OPEN P_RECORDSET FOR
    'SELECT * FROM AAD.'|| PARAM_1 || ' WHERE ID = :PARAM_2' using param_2;

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Expert Comment

by:sdstuber
ID: 34973231
your exception handler doesn't apply,  the OPEN won't generate no_data_found, that exception only applies to implicit SELECT cursors and FETCH of explicit cursors
0
 

Author Comment

by:lulubell-b
ID: 34973270
So the entire statement will be

  OPEN P_RECORDSET FOR
    'SELECT * FROM AAD.'|| PARAM_1 || ' WHERE ID = :PARAM_2' USING PARAM_2;

Not

  OPEN P_RECORDSET FOR
    'SELECT * FROM AAD.'|| PARAM_1 || ' WHERE ID = :PARAM_2';

Correct?

Thank you about the exception statement. I start off with that clause as a template. I will remove it.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34973335
yes,  you need the USING clause to pass the param_2 into the bind variable.

you "could" append the value directly into the sql statement
but your DBA will thank you if you DON'T do that.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34973346
on the other hand,  if PARAM_2 won't change for each table, then you might want to consider embedding it as a literal since it's not really variable with respect to each statement
0
 

Author Comment

by:lulubell-b
ID: 34973418
Thank you, I putting together a ref cursor for the developer to select data from their stage tables

PARAM_2 will change based upon what the application passes.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now