oracle - 绑定(bind)变量的使用

标签 oracle bind-variables

我们可以在过程或函数中使用 oracle 中的绑定(bind)变量吗?

我正在尝试更新我的过程中的绑定(bind)变量。在任何情况下我都可以这样做吗?

if (condition) then
    :v_bind:=10;
end if;

我可以在过程或函数中执行上述操作吗?
variable v_bind number; 
create procedure abc as v_one 
BEGIN 
  select count(a) into v_one from ab; 
  if(v_one<>0) then 
     :v_bind:=10; 
  end if; 

我能做到吗?它向我显示了错误的变量 v_bind

最佳答案

您不能创建包含绑定(bind)变量的过程,因为存储过程是服务器端对象,而绑定(bind)变量仅存在于客户端。

假设我正在使用 SQL*Plus,并且我已经创建了一些绑定(bind)变量。一旦我退出 SQL*Plus,我创建的任何绑定(bind)变量都不再存在。但是,存储过程必须持久保存在数据库中,因此它们不能引用在客户端上创建然后销毁的任何内容。

这是一个示例,显示您无法创建引用绑定(bind)变量的过程:

SQL> variable i number
SQL> exec :i := 0;    

PL/SQL procedure successfully completed.

SQL> print :i

         I
----------
         0

SQL> create or replace procedure test_proc
  2  as
  3  begin
  4    :i := 9;
  5  end;
  6  /

Warning: Procedure created with compilation errors.

SQL> show errors procedure test_proc;
Errors for PROCEDURE TEST_PROC:

LINE/COL ERROR
-------- -----------------------------------------------------------------
4/3      PLS-00049: bad bind variable 'I'

You can, however, pass a bind variable as an OUT parameter for a procedure. The procedure can then assign a value to the OUT parameter, and this value will then be stored in your bind variable.

Suppose we have the following procedure:

CREATE OR REPLACE PROCEDURE do_stuff (
  p_output    OUT INTEGER
)
AS
BEGIN
  p_output := 6;
END;

我们可以使用它来设置绑定(bind)变量,如下所示:

SQL> 变量 i 编号
SQL> 执行 :i := 0;

PL/SQL 过程成功完成。

SQL>打印:我

一世
----------
0

SQL> exec do_stuff(:i);

PL/SQL 过程成功完成。

SQL>打印:我

一世
----------
6

关于oracle - 绑定(bind)变量的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5209981/

相关文章:

oracle - sqlplus - 在 "IN"子句中使用绑定(bind)变量

php - 使用 PHP 给出日期,使用 SQL 获取数据

c# - EF - 在运行时在 Firebird 和 Oracle 数据库之间切换

SQL——两个结果相除

mysql - 多个分组依据的聚合函数

java - 在Intellij idea Community Edition版本2016.1.1中我可以使用数据库吗?

java - 通过Hibernate获取数据而不传递主键

c# - 具有 Oracle 数组绑定(bind)的 ExecuteReader

oracle - Oracle 绑定(bind)变量未正确使用索引的问题

java - "Escape"绑定(bind)变量?在 JDBC SQL 搜索中