sql 2000 分页存储过程

set  ANSI_NULLS  ON
set  QUOTED_IDENTIFIER  ON
go

--  [SelectBase] 1,1,'( select j.*,u.P_Name from Job j left join Users u on j.UserID=u.ID) T','Flag=0'
             alter   procedure   [ SelectBase ]
            
@PageIndex                 int
            
@PageSize                 int
            
@TableName      nvarchar ( 2000 ), 
            
@Where          nvarchar ( 2000 ) = ''  
            
as  
            
Declare   @rowcount            int  
            
Declare   @intStart            int  
            
Declare   @intEnd           int  
            
declare   @Column1   varchar ( 32 -- 第一列名称
             Declare   @SQl   nvarchar ( 2000 ),  @WhereR   nvarchar ( 1000 ),  @OrderBy   nvarchar ( 1000
            
set   @rowcount = 0  
            
set  nocount  on  
            
if   @Where <> ''  
            
begin  
            
set   @Where = '  and  ' + @Where  
            
end  
            
if   CHARINDEX ( ' order by ' @Where ) > 0  
            
begin  
            
set   @WhereR = substring ( @Where 1 CHARINDEX ( ' order by ' , @Where ) - 1 )     -- 取得条件 
             set   @OrderBy = substring ( @Where CHARINDEX ( ' order by ' , @Where ),  Len ( @Where ))     -- 取得排序方式(order by 字段 方式) 
             end  
            
else  
            
begin  
            
set   @WhereR = @Where  
            
set   @OrderBy = '  order by id asc '  
            
end  
            
set   @SQl = ' SELECT @rowcount=count(*) from  ' + cast ( @TableName   as   nvarchar ( 2000 )) + '  where 1=1  ' + @WhereR  
            
            
exec  sp_executeSql  @SQl ,N ' @rowcount int output ' , @rowcount  output 
            
if   @PageIndex = 0   and   @PageSize = 0      -- 不进行分页,查询所有数据列表 
             begin  
            
set   @SQl = ' SELECT * from  ' + cast ( @TableName   as   nvarchar ( 2000 )) + '  where 1=1  ' + @Where  
            
end  
            
else      -- 进行分页查询数据列表 
             begin  
            
set   @intStart = ( @PageIndex - 1 ) * @PageSize + 1
            
set   @intEnd = @intStart + @PageSize - 1  
            
declare   @PKName   nvarchar ( 50 )
            
if ( len ( @TableName ) > 50 )
            
begin
                
Set   @Column1 = ' ID '
            
end
            
else
            
begin
            
set   @Column1 = col_name ( object_id ( @tableName ), 1 -- 设置第一列名称
             end
            
set   @SQl = ' Create table #tem(Row int identity(1,1) not null,joinRow int)  '   
            
set   @SQl = @SQl + ' insert #tem(joinRow) select  ' + @Column1 + '  from  ' + @TableName   + '  where 1=1  ' + @WhereR  
            
set   @SQl = @SQl + '  select * from  ' + @TableName + '  right join #tem  on  ' + @Column1 + ' =#tem.joinRow  '   
            
set   @SQl = @SQl + '  where #tem.Row between  ' + cast ( @intStart   as   varchar ) + '  and  ' + cast ( @intEnd   as   varchar
            
end  
            
-- PRINT @SQl
             exec  sp_executeSql  @SQl  
            
return   @rowcount  
            
set  nocount  off  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值