首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

简单分页的小疑点,麻烦大家了~

2012-04-16 
简单分页的小问题,麻烦大家了~~我写了一个selecttop5*fromCitywhereCityIdnotin(selecttop5CityIdfromCity

简单分页的小问题,麻烦大家了~~
我写了一个
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)

热点排行