Home > Ask the Oracle Experts > (Archive) SQL and PL/SQL Questions & Answers > Writing a stored procedure that accepts a variable
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Writing a stored procedure that accepts a variable

Azim Fahmi EXPERT RESPONSE FROM: Azim Fahmi

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 19 January 2005
I would like to write a stored procedure that accepts a variable and uses that as the table name in a select statement. What I have below will not compile.

 
CREATE OR REPLACE PROCEDURE USP_STAT_FEATURE_COUNT (
         P_TABLENAME                                 IN  VARCHAR2
        ,P_PPMS                                      IN  VARCHAR2
        ,P_FEATURE_COUNT                        OUT NUMBER
)
IS

/******************************************************************************
   NAME:       USP_STAT_FEATURE_COUNT
   PURPOSE: Count of all drawings in the system ******************************************************************************/

TBL varchar2(50);

BEGIN

TBL := P_TABLENAME;

 SELECT COUNT(OBJECTID) INTO P_FEATURE_COUNT FROM P_TABLENAME WHERE PPMS = P_PPMS;

COMMIT;

END USP_STAT_FEATURE_COUNT;
/

>
EXPERT RESPONSE
You cannot do this! Why, you ask. Because on one hand you want the SQL statement to be dynamic based on the Table Name and a column value, on the other end you are using an STATIC SQL statement. Also, you only need COMMIT if you want to make any database changes and not if you are SELECTing data into a PL/SQL variable.

You need to use Native dynamic SQL (or DBMS_SQL if it is more complicated) to achieve your objectives.

Okay here is a version that will compile.

 
CREATE OR REPLACE PROCEDURE USP_STAT_FEATURE_COUNT (
         P_TABLENAME                                 IN  VARCHAR2
        ,P_PPMS                                      IN  VARCHAR2
        ,P_FEATURE_COUNT                        OUT NUMBER
)
IS

/******************************************************************************
   NAME:       USP_STAT_FEATURE_COUNT
   PURPOSE: Count of all drawings in the system ******************************************************************************/

--TBL varchar2(50);

BEGIN

-- TBL := P_TABLENAME;  This statement is unnecessary

 --  You had P_TABLE_NAME  Should be consistent with the
--  parameter name which does not have an underscore 
--  character between "TABLE" and "NAME"
 
     EXECUTE IMMEDIATE ' SELECT COUNT(OBJECTID) FROM ' ||  P_TABLENAME  || ' WHERE PPMS = ' ||  P_PPMS
 INTO P_FEATURE_COUNT;

--  COMMIT  You do not need to commit; 

END USP_STAT_FEATURE_COUNT;
/ 


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Oracle PL/SQL
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams
ORA-01422 error when procedure returns more than one row
Calling procedure inside another procedure in anonymous block
How to import comma-delimited text file to Oracle table
Oracle updates Microsoft developer tools
PLS-00103 errors
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers

(Archive) SQL and PL/SQL
ORA-01654 even though maxextents defined as unlimited
Read, process and write a text file to the database
Concatenating a null character affecting performance
Generating a 10 character alphanumeric sequence
Working with InterDev and DreamWeaver
Line placement in an EMESSAGE
Searching for stored reservation dates in a database
Specifying a column as NULL
Write a SQL script with optional arguments
Error message while trying to write a Pl/SQL block

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts