通用分頁儲存過程

2021-06-15 21:01:03 字數 2335 閱讀 9221

create procedure select_pagesize( @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)--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 @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=''

endelse

begin

select @new_where=' and '+@where

select @newin_where=' where '+@where

endif @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

通用儲存過程 分頁儲存過程

名稱 spall returnrows 輸入 輸出 呼叫 exec spall returnrows select from 表名 頁號,返回記錄數,主鍵 排序字段 spall returnrows select from all categories 2,10,id id 說明 百萬級 通用儲存過...

通用分頁儲存過程

create procedure sp page strtable varchar 50 表名 strcolumn varchar 50 按該列來進行分頁 intcoltype int,strcolumn列的型別,0 數字型別,1 字元型別,2 日期時間型別 intorder bit,排序,0 順序...

通用分頁儲存過程

set quoted identifier on goset ansi nulls off go declare p1 int set p1 null exec sp pageview tbname n v question list fieldkey n id pagecurrent 3,page...