--再搬个实例给你看看.--分页处理实例--数据测试环境--检查对象是否存在,如果存在,删除
if exists (select * from dbo.sysobjects where id = object_id(N'[主表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [主表]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[从表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [从表]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_qry]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_qry]
GO--创建表环境
create table 主表(id varchar(40) not null constraint PK_主表 primary key
,createdate datetime
)create table 从表(id varchar(40) not null
,indexid varchar(40) not null
,value varchar(500)
)
alter table 从表 add constraint PK_从表 PRIMARY KEY CLUSTERED (id,indexid)--添加级联更新
ALTER TABLE dbo.从表 ADD CONSTRAINT
FK_从表_主表 FOREIGN KEY
(
id
) REFERENCES dbo.主表
(
id --主表id
) ON UPDATE CASCADE
 ON DELETE CASCADE--插入测试数据
insert into 主表
select '01','2003-10-21'
union all select '02','2003-10-21'insert into 从表
select '01','1001','asdf'
union all select '01','aby6','abcd'
union all select '01','1003','jsfk'
union all select '01','1t0b','yuet'
union all select '02','1001','jdjd'
union all select '02','1002','ksks'
union all select '02','aby6','hyei'
go
--创建分页查询的存储过程
create proc p_qry
@where varchar(8000)='', --查询的条件,属于主表的字段用a.字段,属于从表的字段用b.字段
@pagesize int=20, --每页的大小
@page int=1, --要查询第几页
@createview bit=1 --是否重建视图,第一次调用时或查询条件变化时指定为1,其他情况指定为0
as
declare @sql varchar(8000)
declare @viewname sysname
set @viewname='tmp_qry_'+host_name()+'_'+user_name() --以用户电脑名+登陆的用户名做视图名
if object_id(@viewname) is null
goto lb_createview
else if @createview=1
begin
set @sql='drop view ['+@viewname+']'
exec(@sql)
goto lb_createview
end
goto lb_qrylb_createview:
if @where<>'' set @where='where ('+@where+')'
exec('create view ['+@viewname+']
as 
select a.*,b.indexid,b.value from 主表 a inner join 从表 b on a.id=b.id
'+@where)lb_qry:
declare @p1 varchar(20),@p2 varchar(20)
if @page=1
begin
set @p1=cast(@pagesize as varchar)
exec('select top '+@p1+' * from ['+@viewname+']')
end
else
begin
select @p1=cast(@pagesize as varchar)
,@p2=cast((@page-1)*@pagesize as varchar)
exec('select top '+@p1+' * from ['+@viewname+'] a left join
(select top '+@p2+' id,indexid from ['+@viewname+']) b
on a.id=b.id and a.indexid=b.indexid
where b.id is null
')
end
go--调用测试
exec p_qry @where='',@pagesize=5,@page=2exec p_qry @where='',@pagesize=5,@page=1,@createview=0  --第二次调用时,因为没有改变查询条件,所以不用再次创建视图.
--带条件的调用
exec p_qry @where='b.indexid=''1002'' and b.value like ''%abcd%'' and a.createdate between ''2003-10-21'' and ''2003-11-21'''
,@pagesize=5,@page=2
exec p_qry @pagesize=5,@page=1,@createview=0   --第二次调用,所以也不用再写条件,只需要设置要调用第几页就行了.

解决方案 »

  1.   


    select top N-M * from your_tab where [id] not in
     (select top M id from your_tab)
      

  2.   

    设表为T,M=5,N=20;
    以下批处理语句就能得到第M到第N行的数据。select top 20 * into #MM
    from T
    alter table #MM
    add autoid int identity
    go
    select top 15 #MM.除autoid这外的所有列
    from #MM
    order by autoid desc本来想弄个存储过程,但是top之后好像不能跟变量的(不记得在哪里问过一个高手了)。
    请问邹大版主,top之后能跟变量吗?还有,更改表后是不是要加上关键字go呢?
      

  3.   

    --ft! top后面是不能跟变量的。可以用exec完成。
    DECLARE @topn varchar(10)
    SET @topn = CONVERT(VARCHAR(10), <你的INT型变量>)
    EXEC('SELECT TOP '+@topn+' FROM <你的表>')