Home > Database > Oracle > Does Oracle have temporary variables?

Does Oracle have temporary variables?

青灯夜游
Release: 2022-04-18 18:05:27
Original
3954 people have browsed it

Oracle has temporary variables. In the Oracle database, you can use variables to write general sql statements. If the "&" and "&&" symbols are used before the variable, then the variable is a temporary variable, and the syntax is "sql statement>&variable name;" . Temporary variables are only valid in the SQL statement using them, and the variable value cannot be retained.

Does Oracle have temporary variables?

The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.

oracle has temporary variables.

In the Oracle database, you can use variables to write general sql statements. When running the sql statement, enter values ​​for the variables, and the variables will be replaced with these values ​​in the sql statement.

Temporary variables are only valid in the SQL statement that uses them. The variable value cannot be retained. Temporary variables are also called replacement variables. In the SQL statement, if the "&" and "&&" symbols are used in front of a variable, it means that the variable is a temporary variable. When the SQL statement is executed, the system will prompt the user to provide a specific data for the variable.

The following is a query statement without using temporary variables:

Does Oracle have temporary variables?

If you use & to declare temporary variables:

SQL> list
  1  select &chang1,ename,job
  2  from scott.emp
  3* where &chang1>&temp
SQL> run
  1  select &chang1,ename,job
  2  from scott.emp
  3* where &chang1>&temp
输入 chang1 的值:  empno
原值    1: select &chang1,ename,job
新值    1: select empno,ename,job
输入 chang1 的值:  empno
输入 temp 的值:  7790
原值    3: where &chang1>&temp
新值    3: where empno>7790
 
     EMPNO ENAME      JOB
---------- ---------- ---------
      7839 KING       PRESIDENT
      7844 TURNER     SALESMAN
      7876 ADAMS      CLERK
      7900 JAMES      CLERK
      7902 FORD       ANALYST
      7934 MILLER     CLERK
Copy after login

Three temporary variables are defined above, but two temporary variables represent the same value. When using a variable defined by &, the value of chang1 is required to be entered twice.

When using &&, if you define If the temporary variable names are the same, you are only required to enter the value once.

Temporary variables defined using &&:

SQL> run
  1  select &&chang1,ename,job
  2  from scott.emp
  3* where &&chang1>&&temp
输入 chang1 的值:  empno
原值    1: select &&chang1,ename,job
新值    1: select empno,ename,job
输入 temp 的值:  7790
原值    3: where &&chang1>&&temp
新值    3: where empno>7790
 
     EMPNO ENAME      JOB
---------- ---------- ---------
      7839 KING       PRESIDENT
      7844 TURNER     SALESMAN
      7876 ADAMS      CLERK
      7900 JAMES      CLERK
      7902 FORD       ANALYST
      7934 MILLER     CLERK
Copy after login

Through the above comparison, you can find the difference between the temporary variables defined by & and &&, but the above Each time the defined temporary variable is entered, the original value and the new value will be displayed by default. If you do not want it to be displayed, you can use the following command:

SQL> set verify off;
SQL> run
  1  select &&chang1,ename,job
  2  from scott.emp
  3* where &&chang1>&&temp
 
     EMPNO ENAME      JOB
---------- ---------- ---------
      7839 KING       PRESIDENT
      7844 TURNER     SALESMAN
      7876 ADAMS      CLERK
      7900 JAMES      CLERK
      7902 FORD       ANALYST
      7934 MILLER     CLERK
 
已选择6行。
Copy after login

If you want it to be displayed, you can use:

SQL> set verify on
SQL> run
  1  select &&chang1,ename,job
  2  from scott.emp
  3* where &&chang1>&&temp
原值    1: select &&chang1,ename,job
新值    1: select empno,ename,job
原值    3: where &&chang1>&&temp
新值    3: where empno>7790
 
     EMPNO ENAME      JOB
---------- ---------- ---------
      7839 KING       PRESIDENT
      7844 TURNER     SALESMAN
      7876 ADAMS      CLERK
      7900 JAMES      CLERK
      7902 FORD       ANALYST
      7934 MILLER     CLERK
Copy after login

Recommended tutorial: "Oracle Tutorial"

The above is the detailed content of Does Oracle have temporary variables?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template