除非另外還指定了 TOP 或 FOR XML

2022-03-07 04:05:23 字數 1021 閱讀 4265

執行下面sql 會收到如題的錯誤.這是為什麼呢? 注意紅色部分,它屬於子查詢,再關注出錯資訊.問題出在order by......

select location from

(select *from location where warehouse = n'w3_shanghai'and put_zone = n'aa'and location_type = n'sto'

order by location asc)as t1 where

not exists(select 1 from inventory where(qty>0 or qty_expected>0)and

inventory.location = t1.location and inventory.warehouse = t1.warehouse)

order by t1.location

ok,就是這樣》因為作為子查詢,如果有order ...,規定必須有top,所以紅色部分改成 select top 100 percent * from location where warehouse = n'w3_shanghai' and put_zone = n'aa' and location_type = n'sto' order by location asc 就漂亮地執行啦!

我們可以舉一反三如:

select top 10 id from table ...order by...

select top 100 percent id from table ... order by ...

也許有問.select top 100 percent這是蝦公尺東西啊. 繼續說明:

--返回符合條件的100%的記錄,即所有符合條件的記錄

select top 100 percent *

--返回符合條件的100條記錄,即只返回符合條件的100條記錄

select top 100 *

甚至我們可以這樣寫 select top 30 percent from table ... order by...desc/asc