简单分页的小问题,麻烦大家了~~
我写了一个
select top 5 * from City where CityId not in (select top 5 CityId from City order by CityId)
这个正确
select top 5 * from City where CityId not in (select top 5*2 CityId from City order by CityId)
就报错:
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '2 ' 附近有语法错误。
请问怎么解决啊?
------解决方案--------------------top 後面祇能接一個具體的數字,如果要用變量或者公式的話可以用動態SQL語句
declare @s varchar(8000)
set @s = 'select top 5 * from City where CityId not in (select top ' + ltrim(5*2) + ' CityId from City order by CityId) '
exec(@s)