DB2 中的合并查询
Posted
技术标签:
【中文标题】DB2 中的合并查询【英文标题】:Merge Query in DB2 【发布时间】:2015-05-12 16:57:19 【问题描述】:我需要用非常复杂的计算更新一张表中的几列。
我的 SQL 不够好,所以我尝试将“with”子句与 update 结合使用,但它抛出了错误。
然后我在网上找到了一个建议使用 MERGE 的帖子,所以我想出了 Merge 查询。但是那个也抛出了一个错误。
所以我删除了所有其他列并仅更新一列以消除复杂性,但无济于事仍然错误
下面是我的查询,选择查询里面工作得很好。 请提出建议。
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
错误是
执行 SQL 命令时出错: 合并到........
DB2 SQL 错误:SQLCODE=-199, SQLSTATE=42601, SQLERRMC=SELECT;VALUES, DRIVER=3.61.75 [SQL State=42601, DB Errorcode=-199]
【问题讨论】:
什么 DB2 版本和平台?MERGE
在那个版本中可用吗?
【参考方案1】:
试试这个吧,我想你在选择语句中忘记了你的标识符,因为在“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
【讨论】:
以上是关于DB2 中的合并查询的主要内容,如果未能解决你的问题,请参考以下文章