2012-06-23 27 views
1

註釋DELETE語句並嘗試使用SELECT語句時,代碼運行正常。 請幫助使用連接時DELETE查詢中的錯誤

DELETE FROM 
--select * from 
Site as s 
join 
(select SiteID,Code, Name, Dense_rank() over (partition by Code order by SiteID) as Rank from Site 
) as t 
on s.SiteID = t.SiteID 
WHERE t.Rank != 1 

獲得以下錯誤消息

Msg 156, Level 15, State 1, Line 5 
Incorrect syntax near the keyword 'as'. 
Msg 156, Level 15, State 1, Line 8 
Incorrect syntax near the keyword 'as'. 
+1

這DBMS是什麼? –

+0

Sql服務器,但我認爲這個問題是通用的SQL查詢 –

+0

不,它不是通用的,因爲並非所有的DBMS都支持刪除語句中的JOIN,並且一些語法有完全不同的語法 –

回答

5

你不能別名delete表,但delete可以參考的別名。取而代之的是:

delete from Site as s 
... 

嘗試:

delete from s 
from Site as s 
... 
+0

是的,f.e. '更新s從站點設置col1 = 1作爲s' – Andomar

+0

感謝這麼好的解釋'你不能別名刪除表,但刪除可以引用別名'我在Sql工作超過2 - 3年,並且每次我不得不尋找這個語法,但不知道原因。大概我不會再問同樣的問題。謝謝 –