用存储过程实现取出从第n1条到第n2条中的记录

来源:互联网 发布:淘宝联盟官网登录 编辑:程序博客网 时间:2024/05/22 03:15

 取出从第n1条到第n2条中的记录  
   
  如果有关键:  
  select   *     from   (select   top   n2   *     from   表   )   a  
  where   关键字段   not   in(select   top   n1     关键字段   from   表) 

 

用存储过程实现1:

create proc test
@N1 INT,
@N2 INT
as

--如果没有自增ID

select * ,identity(int,1,1) id1 into #temp from yourtb

select * from #temp where id1 between @N1 and @N2

drop table #temp

 

用存储过程实现2:
if object_id('up_select_top_n_m') is not null  
  drop  proc up_select_top_n_m
go      
create proc up_select_top_n_m  @n1 int,@n2 int  
as    
declare   @sql   varchar(1000)  
set @sql='SELECT   TOP '+cast(@n2-@n1 as varchar)+'   *  
FROM tb  
WHERE id in(SELECT TOP  '+cast(@n2 as varchar)+' id  
             FROM tb
               ORDER BY id)
ORDER BY id DESC'  
exec(@sql)  
go  
   
--调用实例  
 exec   up_select_top_n_m   n1,n2

原创粉丝点击