我很难尝试更新db2表的前100条记录。我是从Squirrel 3.5.0运行查询
如果我这样做:
SELECT *
FROM "CUSTOMERS"
WHERE CITY = 'SAN JOSE'
FETCH FIRST 100 ROW ONLY
它会适当地过滤数据,但是当我这样做时:
UPDATE ( SELECT * FROM "CUSTOMERS" WHERE CITY = 'SAN JOSE' FETCH FIRST 100 ROWS ONLY )
SET NAME = 'MARIO', LASTNAME = 'PEREZ'
它会抛出此错误
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=(;<IDENTIFIER> STATISTICS, DRIVER=3.53.95
SQLState: 42601
ErrorCode: -104
我遵循此链接中建议的方法 http://db2guideonline.blogspot.com/2013/04/select-update-delete-n-number-of-rows.html
请你帮帮我吗?
由于
答案 0 :(得分:1)
更改此类更新后会发生什么。
UPDATE CUSTOMERS
SET NAME = 'MARIO',
LASTNAME = 'PEREZ'
WHERE customer_id IN (SELECT customer_id
FROM CUSTOMERS
WHERE CITY = 'SAN JOSE'
FETCH FIRST 100 ROWS ONLY)
注意:更改表格的customer_id
到identity
或Auto generated
列
答案 1 :(得分:1)
根据我的判断,您的更新查询是有效的(您没有说明您使用的是哪个版本的db2,因此我假设LUW 9.7及更高版本)。可能是squirrel在查询中添加了一些内容,尝试取消选中限制squirrel接收的行数的复选框。
如果你有一些唯一标识行的东西(我在这里假设cust_id)你可以使用合并语句:
merge into CUSTOMERS x
using (
SELECT * FROM CUSTOMERS
WHERE CITY = 'SAN JOSE'
FETCH FIRST 100 ROWS ONLY
) y
on x.cust_id = y.cust_id
when matched then update
SET NAME = 'MARIO', LASTNAME = 'PEREZ';
另一种选择是枚举表中的行,如下所示:
UPDATE (
SELECT x.*, row_number() over () as rn
FROM CUSTOMERS x
WHERE CITY = 'SAN JOSE'
)
SET NAME = 'MARIO', LASTNAME = 'PEREZ'
where rn <= 100;