views:

58

answers:

2

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?

A: 

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.

antony.trupe
not sure I understand. This is basically an update SP, he has basically decided to not update any column with a blank value, therefore, I need to say update with blank without it actually being an empty string
Rob A
wow, I totally did not read the OP. one minute...
antony.trupe
A: 

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

First, a table with a deafult 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.

APC
I made as similar incorrect assumption: that the OP can edit the procedure.
antony.trupe
yes, I can't, but I can always make a suggestion for him to do so. Doubt it will help though. He has basically added a pipe delimited column list that he iterates through if I want to update something to a default value. I feel like it is a horrible implementation from my perspective.
Rob A
agreed, it is not a good environment and I don't mean to complain here, actually really just trying to do exactly as mentioned, solve team problems with a technical approach I suppose. Thanks for the help nonetheless.
Rob A