dbTalk Databases Forums  

UPDATE Problem

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss UPDATE Problem in the comp.databases.ibm-db2 forum.



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

Default UPDATE Problem - 01-27-2011 , 03:59 PM






I am attempting to update one column in a table with the value from a
second table, using the code below:

UPDATE TABLEA
SET (PYMT_BEFORE_MOD) = (SELECT NEW_PYMT_BEFORE_MOD
FROM TABLEB
WHERE TABLEB.ACCOUNT = TABLEA.ACCOUNT)

There are 16 rows out of 600 that meet criteria that I want to
update. But the statement above updated all 600 rows with NULL.

What am I doing wrong?

J

Reply With Quote
  #2  
Old   
Helmut Tessarek
 
Posts: n/a

Default Re: UPDATE Problem - 01-27-2011 , 04:23 PM






Hi Joe,

On 27.01.11 16:59 , Joe wrote:
Quote:
I am attempting to update one column in a table with the value from a
second table, using the code below:

UPDATE TABLEA
SET (PYMT_BEFORE_MOD) = (SELECT NEW_PYMT_BEFORE_MOD
FROM TABLEB
WHERE TABLEB.ACCOUNT = TABLEA.ACCOUNT)

There are 16 rows out of 600 that meet criteria that I want to
update. But the statement above updated all 600 rows with NULL.

What am I doing wrong?
Several things:

1) you are missing the WHERE clause for the UPDATE statement
2) your SELECT statement most likely does not return a single value or returns
NULL

If you do not specify a where clause for the update statement, all rows are
updated.
What do you get when you run the following statement:
SELECT NEW_PYMT_BEFORE_MOD FROM TABLEB
WHERE TABLEB.ACCOUNT = TABLEA.ACCOUNT


--
Helmut K. C. Tessarek
DB2 Performance and Development

/*
Thou shalt not follow the NULL pointer for chaos and madness
await thee at its end.
*/

Reply With Quote
  #3  
Old   
Joe
 
Posts: n/a

Default Re: UPDATE Problem - 01-27-2011 , 05:30 PM



On Jan 27, 3:23*pm, Helmut Tessarek <tessa... (AT) evermeet (DOT) cx> wrote:
Quote:
Hi Joe,

On 27.01.11 16:59 , Joe wrote:

I am attempting to update one column in a table with the value from a
second table, using the code below:

UPDATE TABLEA
SET (PYMT_BEFORE_MOD) = *(SELECT NEW_PYMT_BEFORE_MOD
* * * * * * * * * * * * * FROM TABLEB
* * * * * * * * * * * * * WHERE *TABLEB.ACCOUNT = *TABLEA.ACCOUNT)

There are 16 rows out of 600 that meet criteria that I want to
update. * *But the statement above updated all 600 rows with NULL.

What am I doing wrong?

Several things:

1) you are missing the WHERE clause for the UPDATE statement
2) your SELECT statement most likely does not return a single value or returns
NULL

If you do not specify a where clause for the update statement, all rows are
updated.
What do you get when you run the following statement:
SELECT NEW_PYMT_BEFORE_MOD FROM TABLEB
WHERE *TABLEB.ACCOUNT = *TABLEA.ACCOUNT

--
Helmut K. C. Tessarek
DB2 Performance and Development

/*
* *Thou shalt not follow the NULL pointer for chaos and madness
* *await thee at its end.
*/
When I run that query I get an error message "Error: SQL0206N
"TABLEA.ACCOUNT" is not valid in the context where it is used."
Probably because I am not referencing TABLEA

When I execute this statement: SELECT NEW_PYMT_BEFORE_MOD
FROM TABLEB
JOIN TABLEA ON
TABLEB.ACCOUNT = TABLEA.ACCOUNT

it returns the 16 rows of data that I expect.

So I think you are saying that I need to have an additional WHERE
clause outside of the last right parenthesis?

Reply With Quote
  #4  
Old   
Helmut Tessarek
 
Posts: n/a

Default Re: UPDATE Problem - 01-27-2011 , 05:58 PM



Hi Joe,

On 27.01.11 18:30 , Joe wrote:
Quote:
it returns the 16 rows of data that I expect.

So I think you are saying that I need to have an additional WHERE
clause outside of the last right parenthesis?
update tableA
set (col) = (<new value>)
where <which rows you want to update>

--
Helmut K. C. Tessarek
DB2 Performance and Development

/*
Thou shalt not follow the NULL pointer for chaos and madness
await thee at its end.
*/

Reply With Quote
  #5  
Old   
Serge Rielau
 
Posts: n/a

Default Re: UPDATE Problem - 01-27-2011 , 10:12 PM



On 1/27/2011 4:59 PM, Joe wrote:
Quote:
UPDATE TABLEA
SET (PYMT_BEFORE_MOD) = (SELECT NEW_PYMT_BEFORE_MOD
FROM TABLEB
WHERE TABLEB.ACCOUNT = TABLEA.ACCOUNT)
UPDATE TABLEA
SET (PYMT_BEFORE_MOD) = (SELECT NEW_PYMT_BEFORE_MOD
FROM TABLEB
WHERE TABLEB.ACCOUNT = TABLEA.ACCOUNT)
WHERE EXISTS(SELECT 1 FROM TABLEB
WHERE TABLEB.ACCOUNT = TABLEA.ACCOUNT)

or
MERGE INTO TABLEA USING TABLEB
ON TABLEB.ACCOUNT = TABLEA.ACCOUNT
WHEN MATCHED THEN UPDATE SET PYMT_BEFORE_MOD = NEW_PYMT_BEFORE_MOD

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW, IBM Toronto Lab
Blog: tinyurl.com/SQLTips4DB2
Wiki: tinyurl.com/Oracle2DB2Wiki
Twitter: srielau

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.