Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Substitution variables, part 1
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Substitution variables, part 1

Frank Kulash EXPERT RESPONSE FROM: Frank Kulash

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 23 September 2003

I am a new user of Oracle products and I'm having problems with two commands: ACCEPT and PROMPT. I have changed the order of the line sometimes putting in CHAR sometimes removing it, sometimes putting in PROMPT, sometimes removing it. I would receive errors no matter what I did. I even tried putting the ACCEPT and PROMPT before and after the BEGIN statement.

Here's what I want to do. At the beginning of the script I read beginning and ending dates from the keyboard, and down in the main part of the program I read from a table using a SELECT and testing to see if a date is between the dates that were entered from the keyboard. I am currently getting a ORA-00922 error when I try to execute/compile this script under SQL*Plus.

PROMPT
ACCEPT in_beg_date  PROMPT 'Enter beginning extract date mm/dd/yy '
PROMPT
ACCEPT in_end_date  PROMPT 'Enter ending extract date mm/dd/yy '

DECLARE
   beg_date VARCHAR2(8) := &in_beg_date;
   end_date VARCHAR2(8) := &in_end_date;
   beg_dte  DATE;
   end_dte  DATE;
...
BEGIN
   beg_dte := TO_DATE (beg_date, 'MM/DD/YY');
   end_dte := TO_DATE (end_date, 'MM/DD/YY');
...
   IF (outrec_abs_date >= beg_dte and <= end_dte) THEN
...


>

Actually, it looks like you have a good understanding of the PROMPT and ACCEPT commands. PROMPT tells SQL*Plus to write a line of text the screen. You use two PROMPT commands here; each of them writes a blank line to the screen, which can be useful for vertical spacing. The ACCEPT command waits for the user to enter a value for a substitution variable. The optional PROMPT clause in the ACCEPT command causes a message to be displayed on the same line where the user will type the value. And you're right in putting the PROMPT and ACCEPT commands outside of the PL/SQL block: they're SQL*Plus commands, not PL/SQL.

You may notice that when you run the script, every line that contains a substitution variable causes two lines of output on your screen, such as:

old   3:    beg_date VARCHAR2(8) := &in_beg_date;
new   3:    beg_date VARCHAR2(8) := 09/01/03;

This shows exactly what SQL*Plus does when it sees a line of code that contains a substitution variable: it replaces the variable name (starting with the ampersand) with the value of that variable. It's exactly the same as if you did a search and replace in text editor like Notepad, and changed every instance of one string (&in_beg_date in this case) into another string (09/09/03). The "old" line is exactly what you have in your code. The "new" line is exactly what will be executed. What would happen if you executed the statement "beg_date VARCHAR2(8) := 09/01/03;"? SQL*Plus would create a string variable and initialize it to the value on the right side of the ":=" sign. In this case, that's a numeric expression, so SQL*Plus would evaluate the expression "(9/1) / 3", and convert the answer to a string, '3'. What you really meant to say is:

   beg_date VARCHAR2(8) := '&in_beg_date';
   end_date VARCHAR2(8) := '&in_end_date';

The single quotes are important.

Click for part 2 of substitution variables.


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



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