今天寫了個SQL語句
select * from (
select ... from A? union ?select ...from B order by B.xx
) as Tmp
SQL查詢分析器中老是報錯:The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.
而單獨執行select ... from A? union ?select ...from B order by B.xx語句時是可以的。
後來分析原因如下:
sql中建view是不能用order by字句的,即:
如果把該查詢語句建成視圖
? create view v_test
? as
? select * from sysobjects order by name
? 會提示出錯:
The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.
而在嵌套查詢中,系統會自動爲select ... from A? union ?select ...from B order by B.xx語句建一個view,裏面有order by字句,當然會報錯了,去掉order by後,運行成功!