使用join语句的DB2更新查询

时间:2019-06-04 07:16:53

标签: sql db2 db2-luw

我正在尝试更新D_WINTEL_HEALTH表,以下是我正在使用的查询

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 

我还尝试了一个查询:

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 

但是它们都返回以下错误:

  

[更新-0行,0.000秒] [错误代码:-104,SQL状态:42601]   DB2 SQL错误:SQLCODE = -104,SQLSTATE = 42601,SQLERRMC = FROM; A SET   A.HEALTH = 7; LEFT_BRACKET,DRIVER = 4.15.82

测试了选择查询是否可以正常工作,不确定问题是什么,在IBM DB2中是否有所不同。

2 个答案:

答案 0 :(得分:1)

改为使用MERGE语句。

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;

答案 1 :(得分:0)

尝试以下查询。

 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