2010-03-30 71 views

回答

1

啓動一個事務,刪除然後回滾;

在PSQL:

test1=> begin; 
BEGIN 
test1=> delete from test1 where test1_id = 1; 
DELETE 2 
test1=> rollback; 
ROLLBACK 

在pgAdmin的(在 「歷史記錄」 選項卡上的 「輸出窗格」):

-- Executing query: 
begin; 
Query returned successfully with no result in 16 ms. 

-- Executing query: 
delete from test1 where test1_id = 1; 
Query returned successfully: 2 rows affected, 16 ms execution time. 

-- Executing query: 
rollback; 
Query returned successfully with no result in 16 ms. 
1

我不知道如何自動做到這一點,但你總是可以做一個選擇然後刪除。

SELECT COUNT(*) FROM foo WHERE delete_me=true; 

DELETE FROM foo WHERE delete_me=true; 
1

正如安德魯說,做互動管理的時候,你可以僅僅通過更換SELECT COUNT(*)DELETE

如果要將此信息在計劃你的之後執行DELETE),許多編程語言爲此提供一個結構。例如,在PHP中,它是pg_affected_rows,而在.NET中,它的返回值爲ExecuteNonQuery

0

使用RETURNING和獲取結果,就像您取一個SELECT - 結果:

DELETE FROM test1 WHERE test1_id = 1 RETURNING id; 

這適用於版本8.2