SQL PLUS一般應用

Using the & Substitution Variable
Use a variable prefixed with an ampersand (&) to
prompt the user for a value.

SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num; 

Use single quotation marks for date and character
values. 

Defining Substitution Variables
• You can predefine variables using the iSQL*Plus
DEFINE command.
DEFINE variable = value creates a user
variable with the CHAR data type.
• If you need to predefine a variable that includes
spaces, you must enclose the value within single
quotation marks when using the DEFINE
command.
• A defined variable is available for the session

Using the DEFINE Command with
& Substitution Variable
• Create the substitution variable using the DEFINE
command.
• Use a variable prefixed with an ampersand (&) to
substitute the value in the SQL statement.
DEFINE employee_num = 200
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num;

DEFINE and UNDEFINE Commands
• A variable remains defined until you either:
– Use the UNDEFINE command to clear it
– Exit iSQL*Plus
• You can verify your changes with the DEFINE
command.
DEFINE job_title = IT_PROG
DEFINE job_title
DEFINE JOB_TITLE = "IT_PROG" (CHAR)
UNDEFINE job_title
DEFINE job_title
SP2-0135: symbol job_title is UNDEFINED

The DEFINE and UNDEFINE Commands
Variables are defined until you either:
• Issue the UNDEFINE command on a variable
• Exit iSQL*Plus
When you undefine variables, you can verify your changes with the DEFINE command. When you exit
iSQL*Plus, variables defined during that session are lost.

Using the && Substitution Variable
Use the double-ampersand (&&) if you want to reuse
the variable value without prompting the user each
time.
SELECT employee_id, last_name, job_id, &&column_name
FROM employees
ORDER BY &column_name;

Double-Ampersand Substitution Variable
You can use the double-ampersand (&&) substitution variable if you want to reuse the variable value
without prompting the user each time. The user will see the prompt for the value only once. In the example
on the slide, the user is asked to give the value for the column_name variable only once. The value
supplied by the user (department_id) is used both for display and ordering of data.
iSQL*Plus stores the value supplied by using the DEFINE command; it uses it again whenever you
reference the variable name. Once a user variable is in place, you need to use the UNDEFINE command to
delete it.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章