

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of creating oracle command files, including setting environment variables, defining script variables, displaying/printing to the screen, accepting user parameters, formatting user inputs, and creating log files. It covers the use of set, define, prompt, accept, column, and spool commands.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Oracle Practical 8
Page 1 of 2
By now, you have received logons and passwords for your own individual accounts within Oracle. This means that you now have the power to create objects, for projects etc⦠What we cover this week will be very useful in this respect. It does not relate directly to any Lectures, but donāt worry, all of that material will be covered within these tutorials. What we are covering today is the creation of professional Oracle command files.
For example, you might create a command file which when run within SQLplus carries out a few basic tasks, like setting up the SQLplus environment, accepting runtime variables, setting script specific variables, logging, run date recording etc⦠And in the middle of all this, you can specify things you want done, such as Table creation, data inserts etcā¦
The purpose of all this, is so that another person can come along later and examine the scripts, see who created them, and when they were last run (from logs), and if there were any errors during the scripts runā¦
Command Files: Setting environment variables⦠To do this we use SET, which turns a SQLplus (the interface) feature ON or OFF or sets it to a certain value.
SET LINESIZE 400 sets the line width, or the total number of characters that can be displayed before wrapping onto the next line. PAGESIZE 0 sets the number of lines per page. By setting it to 0, we are telling SQLplus that we only want data displayed and never print the column headings. ECHO OFF makes SQLplus echo (display) commands to the screen as they execute within a command file. OFF makes SQLplus execute them without displaying them. Results of queries on the other hand are controlled by TERMOUT. TERMOUT OFF tells SQLplus not to display any contents from a command file, or results from queries within that file. This is useful when you are spooling results to a file, as when Oracle has to spool results to a screen also, performance is degraded, FEEDBACK OFF tells oracle not to tell us how many rows were selected by a query. VERIFY OFF tells SQLplus not to confirm variable substitutions at runtime. Examples will be evident later. (Where the & sign is used).
Note at the end of the script, these environment settings should always be set back to their original values so that SQLplus behaves in a normal fashion. Or else, exit SQLplus, as when these are set like this, they are only valid for the current session.
Command Files: Defining Script Variables⦠This is done with the following syntax: DEFINE SCRIPTNAME = ātest1ā
What we have done here is told SQLplus, that every time it comes across the variable SCRIPTNAME, substitute it with ātest1ā. Note that Case is unimportant when specifying a variable name. Down a few lines further in the file, you will see this variable has been printed with the Ampersand sign preceeding it. This tells SQLplus that we are dealing with a variable, and to substitute it with the value we already specified.
Command Files: Displaying/Printing to screen⦠In this command file, we use PROMPT to tell SQLplus that we want strings printed to screen as instructions or information to the user. Because we have TERMOUT ON and ECHO OFF at this
Oracle Practical 8
Page 2 of 2
stage, the output of the PROMPT command is printed to screen, while the command is not printed itself.
Command Files: Accept User Parameters⦠Using the ACCEPT keyword, SQLplus can also accept information from the user running the script, and assign the information to variables
ACCEPT RUNUSER PROMPT '| Enter Current User (No Spaces): '
This tells SQLplus to print the command to the user, āEnter current user (No Spaces):ā, and to take the input received, and assign it to the variable RUNUSER. Therefore the next time &RUNUSER IS MET, SQLplus will make the substitution with the user supplied value.
Command Files: Default values to proper formats⦠Through the use of straightforward SQL, values provided by the user can be altered to make sure that they are in a certain format, or meet certain conditions. For exampleā¦
Firstly, the COLUMN command, tells SQLplus that we are about to replace the existing value assigned to the RUNACCOUNT command with a new value (even if they look identical). Then with the SQL, we take the old RUNACCOUNT variable provided by the user, and convert it to upper case, or default it to Unknown, if null.
Command Files: Display reformatted values⦠Here we simple display the values inputted by the user, in their new formats. We also use SQL queries to print the System Date and Time. Note the Password variable and how it is treated. Between the reformatting and this query, it is only mentioned here, if it was not entered in the first place.
PROMPT | Are these variables correct? PROMPT | Press Enter to Continue, CTRL + C to escape. PAUSE
This tells SQLplus to pause, and allow the user to confirm whether the script should proceed. Note that sometimes, CTRL + C may need to be pressed twice to escape.
Command Files: Using substitution variables⦠Note with the Connect command and the Spool command, we have to use one variable beside another in conjunction witrh text. When doing this, please use a DOT to separate a variable from another, or text as in SPOOL &runpath.&scriptname._001.log Note that the dot preceeding the file extension LOG is the usual one.
Command Files: Log Files⦠It is a good idea to name your log files after the script that generated it, along with a suffix such as 0001 if more than one log is generated for one script. Also print All relevant variables for a script at the beginning of a log so that a process that may involve many variables that may change, can be tracked and debugged if there is trouble.