toofrellik
toofrellik

Reputation: 1307

DB2 update query with join statement

I am trying to update D_WINTEL_HEALTH table and below is the query i am using

UPDATE  D_WINTEL_HEALTH A
SET A.HEALTH = 7
FROM D_WINTEL_HEALTH A JOIN D_REPORTER_STATUS B ON A.HOST = B.HOST 
WHERE B.ALERTKEY = 'kph_netport_xlzc_ilo' AND 
B.SEVERITY = 0 AND 
A.HEALTH <> 0 

I also tried with one more query:

UPDATE D_WINTEL_HEALTH A SET A.HEALTH = 7
FROM (Select D_WINTEL_HEALTH A JOIN D_REPORTER_STATUS B 
ON lower(A.HOST) = lower(B.HOST)) s
WHERE s.ALERTKEY = 'kph_netport_xlzc_ilo' AND s.SEVERITY = 0 AND s.HEALTH <> 0 

But both of them return below error:

[UPDATE - 0 row(s), 0.000 secs] [Error Code: -104, SQL State: 42601] DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=FROM;A SET A.HEALTH = 7 ;LEFT_BRACKET, DRIVER=4.15.82

tested the select query which works fine, not sure what is the issue, is it different in IBM DB2.

Upvotes: 0

Views: 809

Answers (2)

Jervs
Jervs

Reputation: 344

Try with below query.

 merge into AD_WINTEL_HEALTH A
   using (select SEVERITY, ALERTKEY from D_REPORTER_STATUS) b
    ON A.HOST = B.HOST
when matched and B.ALERTKEY = 'kph_netport_xlzc_ilo'  and B.SEVERITY = 0 and A.HEALTH <> 0 
   then update SET A.HEALTH = 7 

Upvotes: 0

Mark Barinstein
Mark Barinstein

Reputation: 12299

Use MERGE statement instead.

MERGE INTO D_WINTEL_HEALTH A
USING D_REPORTER_STATUS B ON A.HOST = B.HOST 
AND B.ALERTKEY = 'kph_netport_xlzc_ilo' 
AND B.SEVERITY = 0 AND A.HEALTH <> 0 
WHEN MATCHED THEN UPDATE SET HEALTH = 7;

Upvotes: 1

Related Questions