0% found this document useful (0 votes)
35 views7 pages

SQL I - 1x04 FRANZ 17

The document discusses the use of substitution variables in SQL queries. It explains how to use single ampersand (&) and double ampersand (&&) variables to prompt for and reuse values in WHERE clauses, ORDER BY clauses, column expressions, table names, and entire SELECT statements. It also covers defining variables with the DEFINE command and removing them with UNDEFINE, as well as toggling variable display using the VERIFY command. The goal is to add flexibility to SQL queries by substituting values at runtime.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views7 pages

SQL I - 1x04 FRANZ 17

The document discusses the use of substitution variables in SQL queries. It explains how to use single ampersand (&) and double ampersand (&&) variables to prompt for and reuse values in WHERE clauses, ORDER BY clauses, column expressions, table names, and entire SELECT statements. It also covers defining variables with the DEFINE command and removing them with UNDEFINE, as well as toggling variable display using the VERIFY command. The goal is to add flexibility to SQL queries by substituting values at runtime.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

14/11/2016

Lesson Agenda
– Limiting rows with:
• The WHERE clause
• The comparison conditions using =, <=, BETWEEN, IN,
LIKE, and NULL operators
• Logical conditions using AND, OR, and NOT operators
– Rules of precedence for operators in an expression
– Sorting rows using the ORDER BY clause
– Substitution variables
– DEFINE and VERIFY commands

Substitution Variables

... salary = ? …
… department_id = ? …
... last_name = ? ...

I want
to query
different
values.

1
14/11/2016

Substitution Variables
– Use substitution variables to:
• Temporarily store values with single-ampersand (&) and double-
ampersand (&&) substitution
– Use substitution variables to supplement the following:
• WHERE conditions
• ORDER BY clauses
• Column expressions
• Table names
• Entire SELECT statements

Using the Single-Ampersand


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 ;

2
14/11/2016

Using the Single-Ampersand


Substitution Variable

Character and Date Values with


Substitution Variables
• Use single quotation marks for date and character values:

SELECT last_name, department_id, salary*12


FROM employees
WHERE job_id = '&job_title' ;

3
14/11/2016

Specifying Column Names,


Expressions, and Text
SELECT employee_id, last_name, job_id,&column_name
FROM employees
WHERE &condition
ORDER BY &order_column ;

Using the Double-Ampersand


Substitution Variable
• Use 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 ;

4
14/11/2016

Lesson Agenda
– Limiting rows with:
• The WHERE clause
• The comparison conditions using =, <=, BETWEEN, IN,
LIKE, and NULL operators
• Logical conditions using AND, OR, and NOT operators
– Rules of precedence for operators in an expression
– Sorting rows using the ORDER BY clause
– Substitution variables
– DEFINE and VERIFY commands

Using the DEFINE Command


– Use the DEFINE command to create and assign a value
to a variable.
– Use the UNDEFINE command to remove a variable.

DEFINE employee_num = 200

SELECT employee_id, last_name, salary, department_id


FROM employees
WHERE employee_id = &employee_num ;

UNDEFINE employee_num

5
14/11/2016

Using the VERIFY Command


• Use the VERIFY command to toggle the display of the
substitution variable, both before and after SQL
Developer replaces substitution variables with values:
SET VERIFY ON
SELECT employee_id, last_name, salary
FROM employees
WHERE employee_id = &employee_num;

Summary
• In this lesson, you should have learned how to:
– Use the WHERE clause to restrict rows of output:
• Use the comparison conditions
• Use the BETWEEN, IN, LIKE, and NULL operators
• Apply the logical AND, OR, and NOT operators
– Use the ORDER BY clause to sort rows of output:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]] ;

– Use ampersand substitution to restrict and sort


output at
run time

6
14/11/2016

Practice 2: Overview
• This practice covers the following topics:
– Selecting data and changing the order of the rows
that are displayed
– Restricting rows by using the WHERE clause
– Sorting rows by using the ORDER BY clause
– Using substitution variables to add flexibility to your
SQL SELECT statements

You might also like