可以在T-SQL中使用公用表表達式來實現魔術,但是如何在DB2 9.7中完成呢?如何選擇第一行並在IBM DB2 9.7中對其進行更新
1. select the first row in a table
2. Update that specific row
這兩個步驟必須在一個事務中執行。請幫助:)
可以在T-SQL中使用公用表表達式來實現魔術,但是如何在DB2 9.7中完成呢?如何選擇第一行並在IBM DB2 9.7中對其進行更新
1. select the first row in a table
2. Update that specific row
這兩個步驟必須在一個事務中執行。請幫助:)
當您遍歷遊標時,您可以在DB2中使用類似的東西。
下面是一個例子:
begin
declare at_end sqlstate;
declare name anchor emp.name;
declare continue handler for not found
set at_end = TRUE;
declare c cursor for
select tabname from emp
for update;
open c;
fetch c into name;
if at_end <> TRUE then
update emp
set name = 'foo'
where current of c;
end if;
[email protected]
該代碼將只更新從選擇retreived的第一行。
這將在發出select語句時創建一個意圖更新鎖,然後它將轉換爲更新語句中的排它鎖。這使得一旦選擇完成後,沒有其他事務可以修改該行。
不需要CTE,也不需要遊標!簡單的定義,將返回該行作爲一個全選擇查詢,並在其上運行的更新:在測試了DB2的Linux/Unix/Windows的
UPDATE (
SELECT *
FROM schema.table
WHERE thing1 = 'blah'
AND thing2 = 'something'
ORDER BY key
FETCH FIRST ROW ONLY
)
SET thing3 = 'updated'
9.7
有「第一沒有這樣的事行「在關係數據庫的表中。您可以在選擇數據時使用ORDER BY子句,但它無序! –
當然,順序應該包括在內 – user1340582