jrl5365

一个不错的分页存储过程

支持多表联合,时间(多条记录相同)排序,但唯一不太好的就是占用资源的点大,希望有空的时候好好改改

 

 

CREATE procedure p_show2
(
@select_list varchar(1000)=\'*\',--不需要select
@table_name varchar(100),
@where varchar(1000)=\'\',--不需要where
@primary_key varchar(100),--当是表联合时,加表名前缀.
@order_by varchar(200),--需要完整的子句 order by ...
@page_size smallint=20,--每页记录
@page_index int=1,--页索引
@do_count bit=0,
@iPageCount int = 1 OUTPUT,  --总的记录数
@pageCount    int = 1 output  ----查询结果分页后的总页数
)--1只统计总数
as
/*
过程名:通用存储过程分页
使用示例:
单表sql调用:exec select_pagesize \'login_id,login_name\',\'tb_login\',\' login_name like \'\'%%\'\'\',\'login_id\',\' order by login_dt desc\',20,10
多表sql调用:exec select_pagesize \'a.login_id,a.login_name,b.pro_name\',\'tb_login a,tb_code_province b\',\' a.pro_id=b.pro_id and a.login_name like \'\'%%\'\'\',\'a.login_id\',\' order by a.login_dt desc\',20,10
备注:外部程序调用不需要转义单引号
原型结构:select top 20 select_list
  from tablename
  where z_id not in(select z_id from (select top 100 z_id from tablename order by order_by) temptable)
      and ...
  order by order_by

*/
declare @strTmp nvarchar(4000)
declare @QueryStr nvarchar(4000)

set @QueryStr=\'select * from \'+@table_name+\' where \'+@where
print(@QueryStr)
set @strTmp=\'select @iPageCount=count(*) from \'+@table_name+\' where \'+@where
----取得查询结果总数量-----
--print(@strTmp)
exec sp_executesql @strTmp,N\'@iPageCount int out \',@iPageCount out
--print(@backinfo)

declare @tmpCounts int
if @iPageCount = 0
begin
    set @pageCount = 0
end
else
begin
    set @tmpCounts = @iPageCount
    --取得分页总数
    set @pageCount=(@tmpCounts+@page_size-1)/@page_size
    --print(@pageCount)
end


declare @sql_str varchar(8000)
declare @record_min int
declare @new_where varchar(1000),@newin_where varchar(1000)
if @where=\'\'--重新为梳理,此过程时性能的考虑,因此不使用 where 1=1 再追加条件。
begin
select @new_where=\'\'
select @newin_where=\'\'
end
else
begin
select @new_where=\' and \'+@where
select @newin_where=\' where \'+@where
end

if @do_count=1
select @sql_str=\'select count(*) from \'+@table_name+@newin_where
else
if @page_index=1
if @where=\'\'
select @sql_str=\'select top \'+convert(varchar,@page_size)+ \' \'+@select_list+\' from \'+@table_name+\' \'+@order_by
else
select @sql_str=\'select top \'+convert(varchar,@page_size)+ \' \'+@select_list+\' from \'+@table_name+\' where \'+@where+\' \'+@order_by
else
begin
select @record_min=(@page_index-1)*@page_size
select @sql_str=\'select top \'+convert(varchar,@page_size)+\' \'+@select_list+\' from \'+@table_name+\' where \'+@primary_key+\' not in (select \'+stuff(@primary_key,1,charindex(\'.\',@primary_key),\'\')
select @sql_str=@sql_str+\' from (select top \'+convert(varchar,@record_min)+\' \'+@primary_key+\' from \'+@table_name+@newin_where+\' \'+@order_by+\') temptable0000)\'
select @sql_str=@sql_str+@new_where+\' \'+@order_by
end
--print @sql_str
exec(@sql_str)
GO

发表于 2009-03-11 13:25  king007  阅读(67)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2022-01-16
  • 2021-11-12
  • 2022-12-23
  • 2022-02-08
  • 2021-10-11
  • 2022-12-23
  • 2022-01-30
猜你喜欢
  • 2021-10-07
  • 2021-08-24
  • 2022-01-25
  • 2021-12-04
  • 2021-09-25
相关资源
相似解决方案