declare @intCount as int set @intCount = 5 declare @vchSqlCmd varchar(800) set @vchSqlCmd = 'select top ' + cast(@intCount as varchar(20)) set @vchSqlCmd = @vchSqlCmd + ' from yourtable' exec(@vchSqlCmd)
我也想过一个最简单的方法:select 1 union select 2 union select 3 union select 4 union select 5,但如果是10000行的话,那这个SQL也太长了。
declare @b table(id int) declare @a intset @a=5 while @a>=1 begin insert into @b select @a set @a=@a-1 end select * from @b order by id
描述不清楚升序 order by 字段
set @intCount = 5
declare @vchSqlCmd varchar(800)
set @vchSqlCmd = 'select top ' + cast(@intCount as varchar(20))
set @vchSqlCmd = @vchSqlCmd + ' from yourtable'
exec(@vchSqlCmd)
declare @a intset @a=5
while @a>=1
begin
insert into @b select @a
set @a=@a-1
end
select * from @b order by id