Home > Ask the Oracle Database / Applications Experts > 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;
/

>
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
Using Oracle PL/SQL
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
PL/SQL functions and triggers in Oracle
Stored procedures in PL/SQL
How to concatenate rows into a single CLOB in PL/SQL
Working with substitution variables and using EXECUTE IMMEDIATE in PL/SQL
How to open a ref cursor in a PL/SQL procedure
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams

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



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

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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