默认值,oracle sp 调用

发布于 2024-08-23 18:50:39 字数 126 浏览 4 评论 0原文

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

I have an oralcle SP forced on me that will not accept an empty parameter in an update. So if I wanted to set a value back to the default of ('') it will not let me pass in the empty string. Is there a keyword you can use such as default, null, etc that oracle would interpret back to the default specified for a particular column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-08-30 18:50:39

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

首先,一个具有默认值的表...

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 的全部目的是让应用程序开发人员更容易与数据库交互。这包括在必要时足够明智地重写存储过程。用软件构建东西与将铸铁梁焊接在一起之间的最大区别在于,软件具有可塑性并且易于更改。特别是当更改不会改变现有过程的签名或行为时,就是这种情况。

Sometimes things are just as simple as you hope they might be.

First, a table with a default value ...

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>

Next a procedure which updates the default column ...

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>

... but which doesn't work as we would like:

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>

So, let's try adding a DEFAULT option ...

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>

... and lo!

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>

I ran this example on an 11g database. I can't remember when Oracle introduced this exact support for DEFAULT, but it has been quite a while (9i???)

edit

The comments are really depressing. The entire point of building PL/SQL APIs is to make it easier for application developers to interact with the database. That includes being sensible enough to rewrite stored procedures when necessary. The big difference between building something out of software and, say, welding cast-iron girders together is that software is malleable and easy to change. Especially when the change doesn't alter the signature or behaviour of an existing procedure, which is the case here.

柠檬色的秋千 2024-08-30 18:50:39

强加给你的程序:

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

如何使用:

begin
  notEditable(bar=>null);
end;

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

The procedure that's been forced on you:

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

How to use:

begin
  notEditable(bar=>null);
end;

I didn't actually compile, but I believe this is the correct syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文