简体   繁体   English

在DB2中合并查询

[英]Merge Query in DB2

I need to update few columns in one table with the very convoluted calculation. 我需要用非常复杂的计算来更新一张表中的几列。

I'm not good enough in SQL so I tried to use "with" clause in combination with update, but It threw error. 我在SQL中的表现还不够好,所以我尝试将“ with”子句与update结合使用,但抛出错误。

Then I found a post online which suggested to use MERGE so I came up with Merge query. 然后我在网上找到了一个建议使用MERGE的帖子,所以我想到了Merge查询。 But that one was also throwing an error. 但是那也引发了一个错误。

So I removed all other column and updating only one column to remove complexity, but no avail still errors 因此,我删除了所有其他列,仅更新了一个列以消除复杂性,但仍然没有任何错误

Below is my query, select query inside working perfectly fine. 下面是我的查询,选择里面的查询查询效果很好。 Please suggest. 请提出建议。

MERGE INTO TABLE_1 AS O 
USING (
SELECT  ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE
FROM TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID= TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'   
and TABLE_3.Primary_ID=TABLE_1.Primary_ID
and TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
 O.Primary_ID = CORRECT.Primary_ID 
)
WHEN MATCHED THEN
UPDATE 
set O.FLOOR_PRICE =CORRECT.CORRECT_FLOOR_PRICE

Error is 错误是

An error occurred when executing the SQL command: MERGE INTO ........ 执行SQL命令时发生错误:MERGE INTO ........

DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=SELECT;VALUES, DRIVER=3.61.75 [SQL State=42601, DB Errorcode=-199] DB2 SQL错误:SQLCODE = -199,SQLSTATE = 42601,SQLERRMC = SELECT; VALUES,DRIVER = 3.61.75 [SQL State = 42601,DB Errorcode = -199]

Try this instead, I think you forgot your identifier in your select statement because after the "ON" statement "CORRECT.Primary_ID" doesn't associate to anything. 请尝试执行此操作,我认为您在select语句中忘记了标识符,因为在“ ON”语句之后,“ CORRECT.Primary_ID”没有任何关联。

MERGE INTO TABLE_1 as O 
USING (
  SELECT  ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER
    (TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)
    -TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER
    (TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE,
    TABLE_1.Primary_ID AS Primary_ID 
  FROM 
    TABLE_1, TABLE_2,TABLE_3
    WHERE TABLE_2.Primary_ID = TABLE_1.Primary_ID
    AND TABLE_2.option_code = 'FSDS'
    AND TABLE_1.FLOOR_PRICE <> '0.00'   
    AND TABLE_3.Primary_ID=TABLE_1.Primary_ID
    AND TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
 O.Primary_ID = CORRECT.Primary_ID 
)
WHEN MATCHED THEN
UPDATE 
set O.FLOOR_PRICE = CORRECT.CORRECT_FLOOR_PRICE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM