Multi-column update from SAME table, different rows, in Oracle, with not-null column: Receiving error 01407

Leanne

I'm getting an error I do not understand.

I have an Oracle database (11g, I think) with a table that has a multi-column unique identifier. It's a transaction table, taking a before and after snapshot of the data from a different table. So far so good, that's all working fine.

We had a bug in our code and I'm trying to fix the data now that the bug is fixed. The bug resulted in the 'after' snapshot having two columns zeroed out in a circumstance where they should have kept their 'before'. So I'm going to update the 'after' snapshot (identified by a transaction type number) to have the same values in those two columns.

I've found all sorts of great help on the multi-column update thing, and I think I have it, except...it generates an error that it cannot update a not-null column to null, when there is no way I can see a null getting in there. Obviously, I am doing something wrong; I just can't tell what.

In some cases, one column can be null - there's an object type column that defines whether that record can have a null value.

The table has this data in it, say:

object_name      object_type   trans_type   trans_date   quantity    actual_cost
no-quantity      1             1            04/16/2014   {null}      20.00
no-quantity      1             9            04/16/2014   {null}       0.00
needs quantity   2             1            04/16/2014   3           15.00
needs quantity   2             9            04/16/2014   0            0.00 

So I need to update the second row (no-quantity, trans_type 9) to actual_cost of 20; quantity can stay null. And I need the 4th row to be updated to quantity of 3, actual_cost of 15.

Here is the query I'm trying to run that is failing - create and insert statements are below:

update  demo_table new 
set     (quantity, actual_cost) = 
        (
        SELECT  quantity, actual_cost
        FROM    demo_table old
        WHERE   old.object_name = new.object_name
            and old.object_type = new.object_type
            and old.trans_date = new.trans_date
            and old.trans_type = 1
            and new.trans_type = 9
)

When I run this I get:

ORA-01407: cannot update ("myschema"."DEMO_TABLE"."ACTUAL_COST") to NULL

(I've tried the other way - update (select col1, col2, etc) - and the table's not set up right for it. Too bad, because that way looked easier to understand...)

Do I maybe need to have an outer where too? The other posts didn't indicate that I did, and I don't know how I would formulate it anyway.

I even get the not-null error on this attempt:

update  demo_table new 
set     (actual_cost) = 
        (
        SELECT  actual_cost
        FROM    demo_table old
        WHERE   old.object_name = new.object_name
            and old.object_type = new.object_type
            and old.trans_date = new.trans_date
            and old.trans_type = 1
            and new.trans_type = 9
            and old.object_type = 2
)

object_type of 2 doesn't have a null quantity, and anyway I'm not trying quantity in this one...

The select statement I run to check the actual where clause is correct looks like this:

SELECT  old.object_name as old_name, old.object_type as old_type, old.trans_type as old_trans, old.trans_date as old_date,
    new.object_name as new_name, new.object_type as new_type, new.trans_type as new_trans, new.trans_date as new_date,
    old.quantity as old_quantity, old.actual_cost as old_cost, new.quantity as new_quantity, new.actual_cost as new_cost
FROM    demo_table old, demo_table new
WHERE    old.object_name = new.object_name
    and old.object_type = new.object_type
    and old.trans_date = new.trans_date
    and old.trans_type = 1
    and new.trans_type = 9

That gets the right values, the right old/new fields, only 2 rows returned, as I expect.

Here are my create-and-insert statements:

create table demo_table (
    object_name varchar2(30) not null,
    object_type number(3) not null,
    trans_type number(3) not null,
    trans_date timestamp(6) not null,
    quantity number(3),
    actual_cost number(17,2) not null
)

insert into demo_table (object_name, object_type, trans_type, trans_date, quantity, actual_cost)
values (
'no-quantity', 1, 1, '16-APR-14', null, 20
)

insert into demo_table (object_name, object_type, trans_type, trans_date, quantity, actual_cost)
values (
'no-quantity', 1, 9, '16-APR-14', null, 0
)

insert into demo_table (object_name, object_type, trans_type, trans_date, quantity, actual_cost)
values (
'needs quantity', 2, 1, '16-APR-14', 3, 15
)

insert into demo_table (object_name, object_type, trans_type, trans_date, quantity, actual_cost)
values (
'needs quantity', 2, 9, '16-APR-14', 0, 0
)

I hope my problem is clear. I did look around a lot, but I couldn't find anything that looked like it matched quite closely enough. The same-table thing is not really covered, and the not-nullable column is not really covered. (Or rather, it is, but I couldn't see how the problems described affected my situation.)

I know the table setup is, shall we say, far from ideal. Can't fix it, tonight.

Bob Jarvis - Reinstate Monica

First - +1 on the question - very well written, lots of supporting information, and please accept a huge THANK YOU for providing enough information to solve the problem, including table def and statements to populate the table.

The only real issue with the first update was that the line in the WHERE clause which subsets the NEW table should be pulled out of the subquery and put into the WHERE clause of the UPDATE statement, as in:

update  demo_table new 
set     (new.quantity, new.actual_cost) = 
        (
        SELECT  old.quantity, old.actual_cost
        FROM    demo_table old
        WHERE   old.object_name = new.object_name
            and old.object_type = new.object_type
            and old.trans_date = new.trans_date
            and old.trans_type = 1
)
where new.trans_type = 9;

I had to fool with this one for quite a while to figure out what was going on. Interesting problem.

SQLFiddle here.

Share and enjoy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy rows from the same table and update with different ID column and another column

From Dev

Oracle Trigger update column with information from another column in the same table

From Dev

Update table if certain fields are null with related values from a different column

From Dev

identify NULL and update for same key column in oracle

From Dev

remove rows from one table, where a field in a column matches that of same column in different table

From Dev

How can I alter a table in Oracle while adding a column, which is of the same type as a column from a different table

From Dev

Update column of one table from same table

From Dev

Update Column value for all rows in Table where Column Value Is Null?

From Dev

Update column for random value from different table

From Dev

Copy rows from and within same oracle SQL table and changing some column values

From Dev

Update multiple column value from one column of the same table

From Dev

Update multiple column value from one column of the same table

From Dev

PostgreSQL update column from another column in same table

From Dev

Mysql: Update json column with values from different table/column

From Dev

Oracle trigger to update all the rows of table based on the data of updating column

From Dev

remove rows that are same on one column but different on another from a data.table

From Dev

Oracle 10g update table i from table 2 column join error

From Dev

Mysql update all rows value with count of same table column

From Dev

Update multiple rows of same column

From Dev

Select rows with same id but different value in another column in a table

From Dev

RETURNing rows of data differently in same table if a specific column is NULL

From Dev

Comparing corresponding rows from the same column and same table

From Dev

Comparing corresponding rows from the same column and same table

From Dev

update column in all rows with column set to null

From Dev

update column in all rows with column set to null

From Dev

oracle: update a column with not null value

From Dev

Update same data from the same table depending on a column

From Dev

Find out the recently selected rows from a Oracle table and can I update a LAST_ACCESSED column whenever the table is accessed

From Dev

mysql update table column with multiple values from same table

Related Related

  1. 1

    Copy rows from the same table and update with different ID column and another column

  2. 2

    Oracle Trigger update column with information from another column in the same table

  3. 3

    Update table if certain fields are null with related values from a different column

  4. 4

    identify NULL and update for same key column in oracle

  5. 5

    remove rows from one table, where a field in a column matches that of same column in different table

  6. 6

    How can I alter a table in Oracle while adding a column, which is of the same type as a column from a different table

  7. 7

    Update column of one table from same table

  8. 8

    Update Column value for all rows in Table where Column Value Is Null?

  9. 9

    Update column for random value from different table

  10. 10

    Copy rows from and within same oracle SQL table and changing some column values

  11. 11

    Update multiple column value from one column of the same table

  12. 12

    Update multiple column value from one column of the same table

  13. 13

    PostgreSQL update column from another column in same table

  14. 14

    Mysql: Update json column with values from different table/column

  15. 15

    Oracle trigger to update all the rows of table based on the data of updating column

  16. 16

    remove rows that are same on one column but different on another from a data.table

  17. 17

    Oracle 10g update table i from table 2 column join error

  18. 18

    Mysql update all rows value with count of same table column

  19. 19

    Update multiple rows of same column

  20. 20

    Select rows with same id but different value in another column in a table

  21. 21

    RETURNing rows of data differently in same table if a specific column is NULL

  22. 22

    Comparing corresponding rows from the same column and same table

  23. 23

    Comparing corresponding rows from the same column and same table

  24. 24

    update column in all rows with column set to null

  25. 25

    update column in all rows with column set to null

  26. 26

    oracle: update a column with not null value

  27. 27

    Update same data from the same table depending on a column

  28. 28

    Find out the recently selected rows from a Oracle table and can I update a LAST_ACCESSED column whenever the table is accessed

  29. 29

    mysql update table column with multiple values from same table

HotTag

Archive