Why can't I use an alias in a DELETE statement?

依然范特西╮ 提交于 2019-11-26 19:03:52

问题


In SQL Server Compact Edition in Visual Studio 2010 (maybe SQL Server and SQL in general, I don't know), this command works:

DELETE FROM foods WHERE (name IN ('chickens', 'rabbits'))

but this command produces an error of: Error near identifier f. Expecting OUTPUT.

DELETE FROM foods f WHERE (f.name IN ('chickens', 'rabbits'))

回答1:


To alias the table you'd have to say:

DELETE f FROM dbo.foods AS f WHERE f.name IN (...);

I fail to see the point of aliasing for this specific DELETE statement, especially since (at least IIRC) this no longer conforms to strict ANSI. But yes, as comments suggest, it may be necessary for other query forms (eg correlation).




回答2:


The delete statement has strange syntax. It goes like this:

DELETE f FROM foods f WHERE (f.name IN ('chickens', 'rabbits'))


来源:https://stackoverflow.com/questions/11005209/why-cant-i-use-an-alias-in-a-delete-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!