这个问题在数据库版里已经有人问过了,我给出的解答是:
select top n * into #temp from db
select top 10 * from db where id not in
(select id from #temp)
其中temp为临时表。
如想选从n开始的m条记录(M为参数,可在过程中赋值)
则可如下执行:
declare @pageno int
set @pageno=m
set rowcount @pageno
select top n * into #temp from db
select * from db where id not in --此处*不可改为top @pageno(可以试一下)
(select id from #temp)
set rowcount 0
关于这个问题我又连三种方法请参考:
1:游标法(cursor):
delcare @n int,@m int,@icount int
select * into #tmp from tablename
set @icount=1
declare mycursor cursor for
select * from #tmp
open mycursor
fetch next from mycursor
while (@fetch_status=0)
begin
if (@icount<@n) or (@icount>@n+@m-1)
delete from #tmp where current of mycursor
set @icount=@icount+1
end
close mycursor
deallocate mycursor
select * from #tmp
2.通过临时表结构的转变更简单,生成一个临时结构,增加一个字段(identity(1,1)).