默认值,oracle sp调用

时间:2010-02-28 17:33:13

标签: sql oracle stored-procedures plsql

我有一个针对我的口令SP,它不接受更新中的空参数。因此,如果我想将值设置回默认值(''),它将不允许我传入空字符串。是否有一个可以使用的关键字,例如default,null等,oracle会将其解释为为特定列指定的默认值?

2 个答案:

答案 0 :(得分:0)

强迫你的程序:

create or replace procedure notEditable(varchar2 bar) as
begin
  --update statement
  null;
end;

使用方法:

begin
  notEditable(bar=>null);
end;

我实际上没有编译,但我相信这是正确的语法。

答案 1 :(得分:0)

有时事情就像你希望的那样简单。

首先,一个默认值为...的表

SQL> create table t23 (
  2      id number not null primary key
  3      , col_d date default sysdate not null )
  4  /

Table created.

SQL> insert into t23 values (1, trunc(sysdate, 'yyyy'))
  2  /

1 row created.

SQL> select * from t23
  2  /

        ID COL_D
---------- ---------
         1 01-JAN-10

SQL>

接下来更新默认列的过程...

SQL> create or replace procedure set_t23_date
  2      ( p_id in t23.id%type
  3        , p_date in t23.col_d%type )
  4  is
  5  begin
  6      update t23
  7      set col_d = p_date
  8      where id = p_id;
  9  end;
 10  /

Procedure created.

SQL>

...但是我们不希望这样做:

SQL> exec set_t23_date ( 1, null )
BEGIN set_t23_date ( 1, null ); END;

*
ERROR at line 1:
ORA-01407: cannot update ("APC"."T23"."COL_D") to NULL
ORA-06512: at "APC.SET_T23_DATE", line 6
ORA-06512: at line 1


SQL>

所以,让我们尝试添加一个DEFAULT选项......

SQL> create or replace procedure set_t23_date
  2      ( p_id in t23.id%type
  3        , p_date in t23.col_d%type )
  4  is
  5  begin
  6      if p_date is not null then
  7          update t23
  8          set col_d = p_date
  9          where id = p_id;
 10      else
 11          update t23
 12          set col_d = default
 13          where id = p_id;
 14      end if;
 15  end;
 16  /

Procedure created.

SQL>

...而且!

SQL> exec set_t23_date ( 1, null )

PL/SQL procedure successfully completed.

SQL>
SQL> select * from t23
  2  /

        ID COL_D
---------- ---------
         1 28-FEB-10

SQL>

我在11g数据库上运行了这个例子。我不记得Oracle何时引入了对DEFAULT的确切支持,但它已经有一段时间了(9i ???)

修改

评论非常令人沮丧。构建PL / SQL API的全部目的是使应用程序开发人员更容易 与数据库进行交互。这包括在必要时足够明智地重写存储过程。用软件构建一些东西,比如将铸铁梁焊接在一起,最大的区别在于软件具有可塑性且易于更改。特别是当更改不会改变现有过程的签名或行为时,就是这种情况。