Home » SQL & PL/SQL » SQL & PL/SQL » Interesting problem with query (Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production)
Interesting problem with query [message #646651] Wed, 06 January 2016 06:48 Go to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
Hello,

I am facing interesting problem with quite complex sql query insert .. select together with log errors.

I am doing something like this:

insert into table_1 (
col1,
col2,
col3,
col4,
col5)
select
orig_table1.orig_col1 col1,
case
  when orig_table2.orig_val1 like 'AA.%'
    then
      NULL
  when orig_table2.orig_val2 like 'BB.%'
    then
      NULL
end
col2,
case
  when orig_table2.orig_val3 like 'CC.%'
    then
      NULL
  when orig_table2.orig_val4 like 'DD.%'
    then
      'SOME_VALUE'
end
col3,
NULL col4,
orig_table2.orig_col2 col5

from

orig_table1
  inner join orig_table2
    on orig_table1.id = orig_table2.id
where 
some other conditions

log errors
reject limit unlimited
;



The original query is much longer and more complex but I chose most of the operations which are present in original query.
I am getting this error:
SQL Error: ORA-38908: internal error occurred during DML Error Logging
ORA-24328 illegal attribute value
ORA-12899: value too large for column string (actual: string, maximum: string)
38908. 00000 - "internal error occurred during DML Error Logging"
*Cause: An unexpected error occurred while executing recursive SQL
to insert a row into the DML Error Logging table.
*Action: Report this error to Oracle Support.

Analyzing it step by step I found potential problem. It is the case condition where NULL is being returned in every condition (I know, it looks strange but it is prepared for the future development). Once I put to_char(NULL) at least around one NULL in when condition it started to work as expected - data are inserted into table and bad rows are inserted into error log table.

BUT!! So I wanted to solve all NULL appearances by putting to_char, to_date, to_number around it and my query started produce the same errors again.
I am little bit confused. Do anyone of you have clue, what is going on?

Thanks a lot in advance.

[Updated on: Wed, 06 January 2016 07:03]

Report message to a moderator

Re: Interesting problem with query [message #646653 is a reply to message #646651] Wed, 06 January 2016 07:34 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
CASE statements can return multiple datatypes. Which one is used for a given case is determined by the datatype of the first THEN value.
Null can also have multiple datatypes so it doesn't know which to use.

This:
case
  when orig_table2.orig_val1 like 'AA.%'
    then
      NULL
  when orig_table2.orig_val2 like 'BB.%'
    then
      NULL
end
col2,

Is equivalent to this:
NULL col2


Past that you need to show us the actual query, or a simplified version that still throws the same error
Re: Interesting problem with query [message #646655 is a reply to message #646653] Wed, 06 January 2016 08:22 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
OK, thanks. In the meantime, I prepared mockup version and checked that behavior is the same like it is in production:

create table TABLE_1 (
  id number(10) PRIMARY KEY,
  col1 varchar2(8),
  col2 varchar2(8),
  col3 varchar2(8),
  col4 varchar2(8),
  col5 varchar2(8)
);


exec DBMS_ERRLOG.CREATE_ERROR_LOG (dml_table_name => 'TABLE_1');


create table ORIG_TABLE_1 (
  id number(10) PRIMARY KEY,
  orig_col1 varchar2(10),
  orig_col2 varchar2(10),
  orig_col3 varchar2(10),
  orig_col4 varchar2(10),
  orig_col5 varchar2(10)
);

create table ORIG_TABLE_2 (
  id number(10) PRIMARY KEY,
  orig_col1 varchar2(10),
  orig_col2 varchar2(10),
  orig_val1 varchar2(10),
  orig_val2 varchar2(10),
  orig_val3 varchar2(10),
  orig_val4 varchar2(10),
  orig_val5 varchar2(10)
);


insert all
 into ORIG_TABLE_1
  values (1,'John','Smith','34 Tree Rd','Pine','CA')
 into ORIG_TABLE_1
  values (2,'James','Jones','1 Atl Blvd','Ocean','NC')
 into ORIG_TABLE_1
  values (3,'Paul','Taylor','10 Dog St','Kingwood','NJ')
 into ORIG_TABLE_1
  values (4,'Patrick','Brown','45 Forrest','City','MT')

 into ORIG_TABLE_2
  values (1, 'Tall', 'Grey', 'AA.123', 'BD.123', 'CD.123', 'DB.123', 'EE.123')
 into ORIG_TABLE_2
  values (2, 'Thin', 'Green', 'AB.123', 'BA.123', 'CA.123', 'DC.123', 'EA.123')
 into ORIG_TABLE_2
  values (3, 'Small', 'Blue', 'AC.123', 'BB.123', 'CB.123', 'DD.123', 'EB.123')
 into ORIG_TABLE_2
  values (4, 'Big', 'Yellow', 'AD.123', 'BC.123', 'CC.123', 'DA.123', 'EC.123')
select * from dual;


Now insert .. select statements:

This one returns errors:
insert into TABLE_1 (
  id,
  col1,
  col2,
  col3,
  col4,
  col5
)
select
1 id,
ORIG_TABLE_1.orig_col1 col1,
case
  when ORIG_TABLE_2.orig_val1 like 'AA.%'
    then
      NULL
  when ORIG_TABLE_2.orig_val2 like 'BB.%'
    then
      NULL
end
col2,
case
  when ORIG_TABLE_2.orig_val3 like 'CC.%'
    then
      NULL
  when ORIG_TABLE_2.orig_val4 like 'DD.%'
    then
      'SOME VALUE'
end
col3,
NULL col4,
ORIG_TABLE_1.orig_col3

from ORIG_TABLE_1
  inner join ORIG_TABLE_2
    on ORIG_TABLE_1.id = ORIG_TABLE_2.id

where ORIG_TABLE_1.id<10
and ORIG_TABLE_2.id>0
log errors
reject limit unlimited
;


This one returns errors as well (NULL is retyped everywhere):
insert into TABLE_1 (
  id,
  col1,
  col2,
  col3,
  col4,
  col5
)
select
1 id,
ORIG_TABLE_1.orig_col1 col1,
case
  when ORIG_TABLE_2.orig_val1 like 'AA.%'
    then
      to_char(NULL)
  when ORIG_TABLE_2.orig_val2 like 'BB.%'
    then
      to_char(NULL)
end
col2,
case
  when ORIG_TABLE_2.orig_val3 like 'CC.%'
    then
      to_char(NULL)
  when ORIG_TABLE_2.orig_val4 like 'DD.%'
    then
      'SOME VALUE'
end
col3,
to_char(NULL) col4,
ORIG_TABLE_1.orig_col3

from ORIG_TABLE_1
  inner join ORIG_TABLE_2
    on ORIG_TABLE_1.id = ORIG_TABLE_2.id

where ORIG_TABLE_1.id<10
and ORIG_TABLE_2.id>0
log errors
reject limit unlimited
;


And finally this one is ok. It inserts no data into table_1 because of the length but inserts data into error table what is desired:
insert into TABLE_1 (
  id,
  col1,
  col2,
  col3,
  col4,
  col5
)
select
1 id,
ORIG_TABLE_1.orig_col1 col1,
case
  when ORIG_TABLE_2.orig_val1 like 'AA.%'
    then
      NULL
  when ORIG_TABLE_2.orig_val2 like 'BB.%'
    then
      to_char(NULL)
end
col2,
case
  when ORIG_TABLE_2.orig_val3 like 'CC.%'
    then
      NULL
  when ORIG_TABLE_2.orig_val4 like 'DD.%'
    then
      'SOME VALUE'
end
col3,
NULL col4,
ORIG_TABLE_1.orig_col3

from ORIG_TABLE_1
  inner join ORIG_TABLE_2
    on ORIG_TABLE_1.id = ORIG_TABLE_2.id

where ORIG_TABLE_1.id<10
and ORIG_TABLE_2.id>0
log errors
reject limit unlimited
;


I understand that NULL is special so it is good to use cast, to_char etc. but I don't really understand why it return ORA-38908 when all NULLs are cured using to_char(NULL).
Re: Interesting problem with query [message #646656 is a reply to message #646655] Wed, 06 January 2016 09:12 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
I have probably found the source of the problem. Surprisingly it was not entirely because of "case".
Surprisingly the error was because of

to_char(NULL) col4


When I leave to_char everywhere in "case" but remove to_char(NULL) col4 and I only leave NULL or if I use "cast" then ORA-38809 disappears.

If I summarize it:

This is OK:

...snip...
NULL col4,
...snip...


This is OK:

...snip...
cast(NULL as varchar2(8)) col4,
...snip...


But this is not OK:

...snip...
to_char(NULL) col4,
...snip...



Is anyone capable to explain this behavior?
Re: Interesting problem with query [message #646657 is a reply to message #646655] Wed, 06 January 2016 09:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
[oracle@localhost ~]$ oerr ora 38908
38908, 00000, "internal error occurred during DML Error Logging" 
// *Cause: An unexpected error occurred while executing recursive SQL
//         to insert a row into the DML Error Logging table.
// *Action: Report this error to Oracle Support.
Re: Interesting problem with query [message #646658 is a reply to message #646657] Wed, 06 January 2016 09:47 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Looks like an oracle bug
Re: Interesting problem with query [message #646713 is a reply to message #646658] Thu, 07 January 2016 06:32 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
OK, thanks. It seems to me too like it is a bug. Is anyone willing to try it on some different oracle version? Wink
Re: Interesting problem with query [message #646717 is a reply to message #646713] Thu, 07 January 2016 07:47 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
You get the same problem on 11.2.0.3.0
Re: Interesting problem with query [message #646719 is a reply to message #646717] Thu, 07 January 2016 07:48 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
To be honest this seems fairly obscure so it wouldn't surprise me if it's been that way for a while
Re: Interesting problem with query [message #646722 is a reply to message #646717] Thu, 07 January 2016 08:01 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
Thanks, cookiemonster
Re: Interesting problem with query [message #646724 is a reply to message #646722] Thu, 07 January 2016 08:13 Go to previous message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Suggest you report it to oracle
Previous Topic: repeat the previous value to next rows
Next Topic: Joining on optional TABLE OF NUMBER parameters
Goto Forum:
  


Current Time: Mon Aug 31 04:40:44 CDT 2026