dbTalk Databases Forums  

Another newbie question about UPDATE

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Another newbie question about UPDATE in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ole Hansen
 
Posts: n/a

Default Another newbie question about UPDATE - 05-24-2005 , 09:08 AM






Hi !

Another question from this stupid danish oracle-newbie :-)

I have 2 tables, STOCKTABLE and STOCKTABLE_EXT

I want to update 2 fields in STOCKTABLE, with the values of 2 fields
in STOCKTABLE_EXT.

STOCKTABLE has the following fields:
Itemnumber ItemName Itemgroup
---------- ------------ ---------
1 Hammer 10
2 Screwdriver 10
3 saw 10


STOCKTABLE_EXT has the following fields:
Itemnumber ItemName ItemGroup
---------- ------------ ---------
1 Book 20
2 Knife 20
4 Fork 30

I want to update STOCKTABLE, so the result will be:
Itemnumber ItemName Itemgroup
---------- ------------ ---------
1 Book 20
2 Knife 20
3 saw 10

I have tried:

UPDATE STOCKTABLE st
SET (st.ItemName, st.ItemGroup) =
(SELECT ste.ItemName, ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But i get an ORA-01407 "Cannot update STOCKTABLE.ItemName to NULL".

So obviously there is something wrong with my statement. What am i
doing wrong ?

Of course i could try:

UPDATE STOCKTABLE st
SET st.ItemName = (SELECT ste.ItemName
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber),
SET st.ItemGroup = (SELECT ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber),
where exists (SELECT ste.ItemNumber
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But this don't seem right.

Anyone has better idea ?

Regards,
Ole

Reply With Quote
  #2  
Old   
DA Morgan
 
Posts: n/a

Default Re: Another newbie question about UPDATE - 05-24-2005 , 11:06 AM






Ole Hansen wrote:
Quote:
Hi !

Another question from this stupid danish oracle-newbie :-)

I have 2 tables, STOCKTABLE and STOCKTABLE_EXT

I want to update 2 fields in STOCKTABLE, with the values of 2 fields
in STOCKTABLE_EXT.

STOCKTABLE has the following fields:
Itemnumber ItemName Itemgroup
---------- ------------ ---------
1 Hammer 10
2 Screwdriver 10
3 saw 10


STOCKTABLE_EXT has the following fields:
Itemnumber ItemName ItemGroup
---------- ------------ ---------
1 Book 20
2 Knife 20
4 Fork 30

I want to update STOCKTABLE, so the result will be:
Itemnumber ItemName Itemgroup
---------- ------------ ---------
1 Book 20
2 Knife 20
3 saw 10

I have tried:

UPDATE STOCKTABLE st
SET (st.ItemName, st.ItemGroup) =
(SELECT ste.ItemName, ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But i get an ORA-01407 "Cannot update STOCKTABLE.ItemName to NULL".

So obviously there is something wrong with my statement. What am i
doing wrong ?

Of course i could try:

UPDATE STOCKTABLE st
SET st.ItemName = (SELECT ste.ItemName
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber),
SET st.ItemGroup = (SELECT ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber),
where exists (SELECT ste.ItemNumber
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But this don't seem right.

Anyone has better idea ?

Regards,
Ole
Please tell me that, and I'm looking at the calendar and know that I am
giving my students their final tonight, this isn't school work.

Simply put ... your update syntax is wrong. Look up the correct syntax
which you can find in your book, in your notes, at tahiti.oracle.com,
at psoug.org and numerous other places.
--
Daniel A. Morgan
http://www.psoug.org
damorgan@x.washington.edu
(replace x with u to respond)


Reply With Quote
  #3  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: Another newbie question about UPDATE - 05-26-2005 , 08:10 AM



Ole Hansen schrieb:
Quote:
Hi !

Another question from this stupid danish oracle-newbie :-)

I have 2 tables, STOCKTABLE and STOCKTABLE_EXT

I want to update 2 fields in STOCKTABLE, with the values of 2 fields
in STOCKTABLE_EXT.

STOCKTABLE has the following fields:
Itemnumber ItemName Itemgroup
---------- ------------ ---------
1 Hammer 10
2 Screwdriver 10
3 saw 10


STOCKTABLE_EXT has the following fields:
Itemnumber ItemName ItemGroup
---------- ------------ ---------
1 Book 20
2 Knife 20
4 Fork 30

I want to update STOCKTABLE, so the result will be:
Itemnumber ItemName Itemgroup
---------- ------------ ---------
1 Book 20
2 Knife 20
3 saw 10

I have tried:

UPDATE STOCKTABLE st
SET (st.ItemName, st.ItemGroup) =
(SELECT ste.ItemName, ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But i get an ORA-01407 "Cannot update STOCKTABLE.ItemName to NULL".

So obviously there is something wrong with my statement. What am i
doing wrong ?

Of course i could try:

UPDATE STOCKTABLE st
SET st.ItemName = (SELECT ste.ItemName
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber),
SET st.ItemGroup = (SELECT ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber),
where exists (SELECT ste.ItemNumber
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But this don't seem right.

Anyone has better idea ?

Regards,
Ole
http://download-west.oracle.com/docs...8a.htm#2067717

<quote>
subquery

Specify a subquery that returns exactly one row for each row updated.

* If you specify only one column in the update_set_clause, then the
subquery can return only one value.
* If you specify multiple columns in the update_set_clause, then
the subquery must return as many values as you have specified columns.

You can use the flashback_clause of within the subquery to update table
with past data.
See Also:

the flashback_clause of SELECT for more information on this clause

If the subquery returns no rows, then the column is assigned a null.
</quote>

In your first example you didn't get any rows for itemnumber 3, there is
obviously ( if not , your update would succeed and you get a result
which you probably don't expect ) not null constraint on itemname and
you got ORA 1407. In your second example you restrict the rows that
should be updated, that way it works properly. In general there are many
ways to write an update based on subquery, but you should always
(never say always - maybe you wish update all rows? ;-) restrict the
rows that should be updated in WHERE clause of UPDATE statement,
subquery doesn't do that , it delivers only new values for updated rows.

Best regards

Maxim


Reply With Quote
  #4  
Old   
Ed Prochak
 
Posts: n/a

Default Re: Another newbie question about UPDATE - 05-26-2005 , 11:56 AM





Ole Hansen wrote:
Quote:
Hi !

Another question from this stupid danish oracle-newbie :-)

I have 2 tables, STOCKTABLE and STOCKTABLE_EXT

I want to update 2 fields in STOCKTABLE, with the values of 2 fields
in STOCKTABLE_EXT.

[]
I have tried:

UPDATE STOCKTABLE st
SET (st.ItemName, st.ItemGroup) =
(SELECT ste.ItemName, ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But i get an ORA-01407 "Cannot update STOCKTABLE.ItemName to NULL".

So obviously there is something wrong with my statement. What am i
doing wrong ?
Given that error message why do you think the problem is in your
statement? I would first look in the data. Is there a row (or many) in
STOCKTABLE_EXT that has a NULL ItemName??

Ed
Two proverbs:
1. Sometimes it doesn't pay to overlook the obvious.
2. Know your data.



Reply With Quote
  #5  
Old   
Gerard H. Pille
 
Posts: n/a

Default Re: Another newbie question about UPDATE - 05-26-2005 , 02:57 PM



Ed Prochak wrote:
Quote:
Ole Hansen wrote:

Hi !

Another question from this stupid danish oracle-newbie :-)

I have 2 tables, STOCKTABLE and STOCKTABLE_EXT

I want to update 2 fields in STOCKTABLE, with the values of 2 fields
in STOCKTABLE_EXT.


[]

I have tried:

UPDATE STOCKTABLE st
SET (st.ItemName, st.ItemGroup) =
(SELECT ste.ItemName, ste.ItemGroup
FROM STOCKTABLE_EXT ste
WHERE st.itemnumber = ste.itemnumber)

But i get an ORA-01407 "Cannot update STOCKTABLE.ItemName to NULL".

So obviously there is something wrong with my statement. What am i
doing wrong ?


Given that error message why do you think the problem is in your
statement? I would first look in the data. Is there a row (or many) in
STOCKTABLE_EXT that has a NULL ItemName??

Ed
Two proverbs:
1. Sometimes it doesn't pay to overlook the obvious.
2. Know your data.

LOL
would it not be that there are rows in STOCKTABLE without data in STOCKTABLE_EXT?

He should have kept his where-clause from his first try.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.