SQL Fundamentals:Substitution Variables(替代变量)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Fundamentals:Substitution Variables(替代变量)相关的知识,希望对你有一定的参考价值。

Substitution Variables(替代变量)

WHERE子句中,通过使用替代变量来代替精确的值,就可以用不同的值运行同一个查询.

  • 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(&

When a SQL query has to be executed more than once for the different set of inputs, substitution variables can be used. Substitution variables can be used to prompt for user inputs before the query execution. They are widely used in query based report generation which takes data range from the users as input for the conditional filtering and data display. Substitution variables are prefixed by a single-ampersand (&) symbol to temporarily store values. For example,

 

SELECT EMPLOYEE_ID, LAST_NAME, SALARY
FROM employees
WHERE LAST_NAME =‘&last_name
OR EMPLOYEE_ID = ‘&EMPNO’;

 

When the above SELECT query is executed, oracle identifies the ‘&‘ as substitution variable. It prompts user to enter value for ‘last_name‘ and ‘EMPNO‘ as below.

 

Enter value for last_name:
Enter value for empno:

 

Once the user provides inputs to both the variables, values are substituted, query is verified and executed.

 

Points to be noted -

  • If the variable is meant to substitute a character or date value, the literal needs to be enclosed in single quotes. A useful technique is to enclose the ampersand substitution variable in single quotes when dealing with character and date values. 字符串值和日期值需要用单引号引起来,所以要将替代变量用单引号引起来.
  • Both SQL Developer and SQL* Plus support the substitution variables and the DEFINE/UNDEFINE commands. Though SQL Developer or SQL* Plus does not support validation checks (except for data type) on user input.
  • You can use the substitution variables not only in the WHERE clause of a SQL statement, but also as substitution for column names, expressions, or text.
  • 也可以将替代变量用作列名、表达式、文本

如果不输入替代变量的值,将会得到一个错误

替代变量可用于SELECT语句的任何地方,除了在命令提示符后作为第一个单词输入.

 

Using the Double-Ampersand Substitution Variable(&&

&&用户只需要输入一次就可以了,&用户需要每次输入

如果想重用变量值而不用每次提示用户进行输入,可以使用两个&&的替代变量.

SQL> SELECT ename,&&column_name

 2 from emp

 3 order by &&column_name;

 

Enter value for column_name: job

old   1: SELECT ename,&&column_name

new   1: SELECT ename,job

old   3: order by &&column_name

new   3: order by job

When the same substitution variable is used at more than one place, then to avoid re-entering the same data again, we use double ampersand substitution. In such cases, value of the substitution variable, once entered, would be substituted at all instants of usage.

 

SELECT first_name, HIRE_DATE, SEPARATION_DATE
FROM employees
WHERE HIRE_DATE LIKE ‘%&DT%‘ AND SEPARATION_DATE ‘%&&DT%‘

 

Note that the same value of &DT is substituted twice in the above query. So, its value once given by the user will be substituted at two places.

 

SQL Developer stores the value that is supplied by using the DEFINE command.

SQL Developer存储使用DEFINE命令所提供的值.

It uses it again whenever you reference the variable name. After a user variable is in place, you need to use the UNDEFINE command to delete it:

 

UNDEFINE colum_name

 

The DEFINE and VERIFY Commands

 

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

定义一个替代变量

Use the UNDEFINE command to remove a variable.

删除一个替代变量

SQL> DEFINE emp_sal=1000

SQL> select ename,job,sal

 2 from emp

 3 where sal>&emp_sal;

old   3: where sal>&emp_sal

new   3: where sal>1000

 

ENAME      JOB              SAL

---------- --------- ----------

WARD       SALESMAN        1250

JONES      MANAGER         2975

 

11 rows selected.

 

SQL> UNDEFINE emp_sal

SQL>

Setting the definition of variables in a session is set by DEFINE feature of SQL* Plus. The variables can be defined in the session, so as to avoid halt during query execution. Oracle reads the same variable whenever encountered in an SQL query. It is in ON state by default. With the help of DEFINE clause, one can declare a variable in command line before query execution as DEFINE variable=value;.

 

Use the VERIFY command to toggle切换 the display of the substitution variable, both before and after SQL Developer replaces substitution variables with values.

要确认SQL语句的变化,使用VERIFY命令

SET VERIFY ON/OFF

 

Verify command verifies the above substitution showing as OLD and NEW statement. It is OFF by default and can be set to ON using SET command.

 

SQL> SET DEFINE ON
SQL> SET VERIFY ON
SQL> DEFINE NAME = MARTIN‘
SQL> SELECT first_name, SALARY
FROM employees
WHERE first_name = ‘&NAME‘;
OLD   1: select first_name, sal from employee where first_name = ‘&first_name‘
new   1: select first_name, sal from employee where first_name = ‘MARTIN‘

first_name     SALARY
-------        -------
MARTIN         5000

 

以上是关于SQL Fundamentals:Substitution Variables(替代变量)的主要内容,如果未能解决你的问题,请参考以下文章

SQL Fundamentals: 子查询 || 分析函数

SQL Fundamentals || DCL(Data Control Language) || 角色ROLES

SQL Fundamentals:Restricting and Sorting Data限制和排序数据

SQL Fundamentals: Basic SELECT statement基本的select语句

SQL Fundamentals || DCL(Data Control Language) || 用户管理&Profile概要文件

SQL Fundamentals || Single-Row Functions || 转换函数 Conversion function