Home > Ask the Oracle Database / Applications Experts > Oracle PL/SQL Questions & Answers > Working with substitution variables and using EXECUTE IMMEDIATE in PL/SQL
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Working with substitution variables and using EXECUTE IMMEDIATE in PL/SQL

Dan Clamage EXPERT RESPONSE FROM: Dan Clamage

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: 16 April 2009
Can I use a variable in place of table name in a SELECT Statement?

Example :

define a = 'EMP';

SELECT  * FROM &A;


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



RELATED CONTENT
Oracle PL/SQL
How to concatenate rows into a single CLOB in PL/SQL
How to open a ref cursor in a PL/SQL procedure
Converting Long Raw to Blob
Parsing in Oracle
ORA-01422 error when procedure returns more than one row
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers
Execute SQL statement from table in other schema
Script to revoke access from user
PL/SQL procedure to load CSV file into database table

Using Oracle PL/SQL
Oracle tutorial library: SearchOracle.com's learning guides
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
Stored procedures in PL/SQL
PL/SQL functions and triggers in Oracle
How to concatenate rows into a single CLOB 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


Substitution variables only work in SQL*Plus or SQL*Worksheet, because those tools know about substitution variables. This is OK for a one-off script you're running manually. For automation, it's not so good because it'll sit there at a prompt waiting for input. Since you define the substitution variable first, simply "double-up" your ampersands:

SQL> define a= 'DUAL';
SQL> select sysdate from &&a;
old   1: select sysdate from &&a
new   1: select sysdate from DUAL

SYSDATE
---------
30-MAR-09
You can use command-line substitution variables &1, &2, &3 etc. in lieu of defining them inline in the script, if you're calling your script from some other context, such as a Korn Shell script.

In PL/SQL, you can do this dynamically with Execute Immediate. This is a trivial example, but it illustrates the general programming style I use with dynamic SQL.

declare
  c_stmt constant varchar2(100) := 'select sysdate from ~t';
  v_stmt varchar2(200);
  v_date date;
begin
  v_stmt := replace(c_stmt, '~t', 'dual');
  dbms_output.put_line(v_stmt);
  execute immediate v_stmt into v_date;
  dbms_output.put_line(v_date);
end;
/

select sysdate from dual
30-MAR-09

I like to build a template of the SQL I'm trying to generate dynamically. I'll code a concrete sample query and test it to make sure it works. Then I use my own "substitution placeholders" (a single character prefixed with tilde) to indicate what I'm replacing. The tilde-character doesn't mean anything; it's just a sequence that's unlikely to appear in an SQL statement.

By using a template, reusing it for different dynamic queries is much easier.

If your dynamic SQL has a syntax error, printing it out before executing it is the most "immediate" way to see the mistake quickly.




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