2010-02-12 216 views
3

我想刪除滿足某些條件的表中的最後一行。tsql - 刪除最後一行

DELETE TOP 1 FROM SOME_TABLE 
WHERE SOME_COULMN = @VALUE 
ORDER BY 1 DESC 
+0

顛倒你的訂單並刪除第一個? – Andrew 2010-02-12 11:17:47

回答

5
DELETE FROM SOME_TABLE 
WHERE UNIQUE_ID = 
(SELECT TOP 1 UNIQUE_ID 
FROM SOME_TABLE 
WHERE SOME_COLUMN = @VALUE 
ORDER BY SOMETHING DESC) 
-1

DELETE和UPDATE語句需要的行參數的數目括號()

DELETE TOP (1) FROM SOME_TABLE WHERE SOME_COULMN = @VALUE ORDER BY 1個DESC