基本上我想這樣做:請解釋這個刪除前100名的SQL語法
delete top(100) from table order by id asc
但MS SQL不允許在這個位置
常見的解決辦法似乎是這個順序:
DELETE table WHERE id IN(SELECT TOP (100) id FROM table ORDER BY id asc)
但是我也發現這個方法here:
delete table from (select top (100) * from table order by id asc) table
它有一個更好的估計執行計劃(74:26)。不幸的是我不太懂語法,請有人向我解釋一下嗎?
對任何其他方法始終感興趣,以達到相同的結果。
編輯︰我仍然沒有得到它我害怕,我希望能夠閱讀查詢,因爲我讀了前兩個實際上是英語。上述查詢我是:
delete the top 100 records from table, with the records ordered by id ascending
delete the top 100 records from table where id is anyone of (this lot of ids)
delete table from (this lot of records) table
我不能改變第三個成一個邏輯的英語句子......我想我想要知道的是,這如何轉成「從表中刪除(這很多記錄)「。 'from'似乎處於不合邏輯的地位,第二次提到'table'在邏輯上是多餘的(對我來說)。
我無法企及你4個語法與第三例子讀我爲「從子表表中刪除表」中的一個。請參閱我的編輯問題。 – Patrick 2010-06-16 13:29:51
@Patrick:你可以在你的查詢中添加可選的'from'(上面的第3個和第4個表格之間的區別),以使其更符合邏輯地閱讀。我調整了上述表格以便更具描述性。 – Guffa 2010-06-16 14:49:23
@Patrick:查詢可以用英語描述爲「從表中刪除所有匹配記錄的記錄(這個記錄稱爲'表')。」請注意,查詢中的最後一個「表」是別名,而不是表的實際名稱。 – Guffa 2010-06-16 15:01:16