|Previous||Table of Contents||Next|
The ttitle Command
The ttitle command instructs SQL*Plus to print a title at the top of each page of a report. You can perform a number of actions using the ttitle command, such as:
ttitle skip n SYSDATE;
ttitle left 'MaldenPower' center 'Quality Control' right SYSDATE; ttitle col 50 'Report';
ttitle tab n 'Malden Power';
ttitle off; ttitle on;
The undefine Command
The undefine command is used to dispose of a variable inside an SQL session. The format of the undefine command is:
Once a variable has been undefined, it can no longer be referenced unless it is redefined.
Youll probably connect to SQL*Plus using a command like:
SQL*Plus provides some alternative methods of connecting that you should be aware of. For instance:
sqlplus [-silent] username/password
Logging into SQL*Plus using the -silent flag instructs the program not to give any informational messages (not even a prompt). This is particularly useful when your script is dynamically generating code or when executing SQL*Plus from inside another piece of software.
If you login and find that you need to be working under another schema, you can login to a new schema without exiting SQL*Plus and reconnecting by using the following command:
You can use any of these login commands without typing your username or password as part of the command; SQL*Plus will prompt you for these values if you dont include them in the command. Using your password as part of a command reveals the password to anyone who sees the command, so its probably better to enter your password when prompted in most situations.
The exit and quit commands are used to leave SQL*Plus and return to the operating system. You should be aware that using either of these commands implicitly commits changes to the database. If youve made changes that you dont want to save, youll have to roll back prior to issuing one of these commands.
You can use this command to return a status to an OS-level application that called the script, as shown in the following example:
There are five values that can be returned: success, failure, warning, integer, and variable.
When you call a script, you can pass parameters to the script in the command line, as shown in the following example:
sqlplus username/password @set_grade 999999999 2103 'A'
This is what the set_grade.sql script looks like:
UPDATE ENROLLED_COURSES SET course_grade = upper ('&&3') WHERE ssn = &&1 AND course_number = &&2;
In this example, 999999999 is referenced in the script as &&1, and 2103 is referenced in the script as &&2. The students grade for course 2103 is referenced as &&3.
Parameters are referenced by integer values in the order in which they are passed (one of the reasons why its important to document what parameters are used and in what order they should be passed).
References to parameters are typically made using the ampersand (&) character; this character can be altered using the set define command. A single ampersand instructs SQL to prompt the user for the value of the parameter. Double ampersands (&&) instruct SQL to prompt the user for a value if there is no value already stored for the parameter; variables defined with a single & are undefined immediately after the variable is used. Consequently, if a script is to run without interaction, you should use double ampersands to reference your parameters.
The spool command is used to control the direction of output to a file. The syntax for the spool command is:
If no extension is given for the spool file, SQL*Plus assumes that you want the output file to have an extension of .LST.
To stop spooling to a file, use the following command:
A substitution variable is a variable name preceded by one or two ampersands, like the variable used in the following example:
SELECT count (*) FROM STUDENTS WHERE last_name = upper (&LastName);
When this script is run, SQL*Plus will prompt the user for each undefined substitution variable it encounters.
The use of IF-THEN-ELSE logic, looping structures, and other features can make writing a powerful script much simpler. Its extremely difficult to simulate these logical control structures using only SQL statements.
|Previous||Table of Contents||Next|