代码之家  ›  专栏  ›  技术社区  ›  Rob Allen

默认值,oracle sp call

  •  2
  • Rob Allen  · 技术社区  · 16 年前

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

    2 回复  |  直到 16 年前
        1
  •  0
  •   APC    15 年前

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

    首先,一个带有默认值的表。。。

    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>
    

    所以,让我们尝试添加一个默认选项。。。

    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>
    

    编辑

    这些评论真令人沮丧。构建PL/sqlapi的关键在于 更容易的

        2
  •  0
  •   antony.trupe    16 年前

    你被迫接受的程序是:

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

    使用方法:

    begin
      notEditable(bar=>null);
    end;
    

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