如何声明变量并在同一个 Oracle SQL 脚本中使用它?

2021-12-05 00:00:00 sql variables oracle declaration

我想写可复用的代码,需要在开头声明一些变量并在脚本中复用,比如:

I want to write reusable code and need to declare some variables at the beginning and reuse them in the script, such as:

DEFINE stupidvar = 'stupidvarcontent';

SELECT stupiddata
FROM stupidtable
WHERE stupidcolumn = &stupidvar;

如何声明一个变量并在随后的语句中重用它,例如在 SQLDeveloper 中使用它.

How can I declare a variable and reuse it in statements that follow such as in using it SQLDeveloper.

尝试

  • 使用 DECLARE 部分并在 BEGINEND; 中插入以下 SELECT 语句.使用 &stupidvar 访问变量.
  • 使用关键字 DEFINE 并访问变量.
  • 使用关键字 VARIABLE 并访问变量.
  • Use a DECLARE section and insert the following SELECT statement in BEGIN and END;. Acces the variable using &stupidvar.
  • Use the keyword DEFINE and access the variable.
  • Using the keyword VARIABLE and access the the variable.

但是我在尝试过程中遇到了各种各样的错误(未绑定变量、语法错误、预期的 SELECT INTO...).

But I am getting all kinds of errors during my tries (Unbound variable, Syntax error, Expected SELECT INTO...).

推荐答案

在 SQL*Plus 脚本中有多种声明变量的方法.

There are a several ways of declaring variables in SQL*Plus scripts.

首先是使用VAR,来声明一个绑定变量.为 VAR 赋值的机制是通过 EXEC 调用:

The first is to use VAR, to declare a bind variable. The mechanism for assigning values to a VAR is with an EXEC call:

SQL> var name varchar2(20)
SQL> exec :name := 'SALES'

PL/SQL procedure successfully completed.

SQL> select * from dept
  2  where dname = :name
  3  /

    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO

SQL>

当我们想要调用具有 OUT 参数或函数的存储过程时,VAR 特别有用.

A VAR is particularly useful when we want to call a stored procedure which has OUT parameters or a function.

或者,我们可以使用替换变量.这些适用于交互模式:

Alternatively we can use substitution variables. These are good for interactive mode:

SQL> accept p_dno prompt "Please enter Department number: " default 10
Please enter Department number: 20
SQL> select ename, sal
  2  from emp
  3  where deptno = &p_dno
  4  /
old   3: where deptno = &p_dno
new   3: where deptno = 20

ENAME             SAL
---------- ----------
CLARKE            800
ROBERTSON        2975
RIGBY            3000
KULASH           1100
GASPAROTTO       3000

SQL>

当我们编写一个调用其他脚本的脚本时,预先定义变量会很有用.此代码段运行时不提示我输入值:

When we're writing a script which calls other scripts it can be useful to DEFine the variables upfront. This snippet runs without prompting me to enter a value:

SQL> def p_dno = 40
SQL> select ename, sal
  2  from emp
  3  where deptno = &p_dno
  4  /
old   3: where deptno = &p_dno
new   3: where deptno = 40

no rows selected

SQL>

最后是匿名 PL/SQL 块.如您所见,我们仍然可以交互地为声明的变量赋值:

Finally there's the anonymous PL/SQL block. As you see, we can still assign values to declared variables interactively:

SQL> set serveroutput on size unlimited
SQL> declare
  2      n pls_integer;
  3      l_sal number := 3500;
  4      l_dno number := &dno;
  5  begin
  6      select count(*)
  7      into n
  8      from emp
  9      where sal > l_sal
 10      and deptno = l_dno;
 11      dbms_output.put_line('top earners = '||to_char(n));
 12  end;
 13  /
Enter value for dno: 10
old   4:     l_dno number := &dno;
new   4:     l_dno number := 10;
top earners = 1

PL/SQL procedure successfully completed.

SQL>

相关文章