SQL分頁儲存過程

2021-06-26 16:14:56 字數 4235 閱讀 8199

sql分頁儲存過程

create procedure [dbo].[up_pagerdata]

(@tables varchar(400),

@primarykey varchar(100),--當是表聯合時,加表名字首.

@sort varchar(200),--需要完整的子句 order by ...

@currentpage int=1,--頁索引

@pagesize smallint=20,--每頁記錄

@fields varchar(1000)='*',--不需要select

@filter varchar(1000)='',--不需要where

@docount bit=0,--1只統計總數

@linecount varchar(20)='0' output)as

/*過程名:通用儲存過程分頁

使用示例:

單錶sql呼叫:exec sp_pagination_test 'tb_login','login_id','order by login_dt desc','1','50','*','login_ist=0 and long_3=0','0'

多表sql呼叫:exec sp_pagination_test 'tb_login a,tb_code_province b','a.login_id',' order by a.login_dt desc',1,20,'*',' a.pro_id=b.pro_id and a.login_name like ''%%''','a.login_id,a.login_name,b.pro_name','a.login_id',0

備註:外部程式呼叫不需要轉義單引號

原型結構: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 @filter=''--重新為梳理,此過程時效能的考慮,因此不使用 where 1=1 再追加條件。

begin

select @new_where=''

select @newin_where=''

endelse

begin

select @new_where=' and '+@filter

select @newin_where=' where '+@filter

enddeclare @ctemp nvarchar(1000)

create table #temp(linecount int)

set @ctemp = 'insert into #temp (linecount) select count(*) from '+@tables+@newin_where

exec (@ctemp)

select @linecount = linecount from #temp

drop table #temp

if @docount=1

select @sql_str='select count(*) from '+@tables+@newin_where

else

if @currentpage=1

if @filter=''

select @sql_str='select top '+convert(varchar,@pagesize)+ ' '+@fields+' from '+@tables+' '+@sort

else

select @sql_str='select top '+convert(varchar,@pagesize)+ ' '+@fields+' from '+@tables+' where '+@filter+' '+@sort

else

begin

select @record_min=(@currentpage-1)*@pagesize

select @sql_str='select top '+convert(varchar,@pagesize)+' '+@fields+' from '+@tables+' where '+@primarykey+' not in (select '+stuff(@primarykey,1,charindex('.',@primarykey),'')

select @sql_str=@sql_str+' from (select top '+convert(varchar,@record_min)+' '+@primarykey+' from '+@tables+@newin_where+' '+@sort+') temptable0000)'

select @sql_str=@sql_str+@new_where+' '+@sort

endprint @linecount

exec(@sql_str)

create procedure [dbo].[up_pagedata2005]

( @tablename varchar(350), --表名

@refieldsstr varchar(3000) = '*', --欄位名(全部欄位為*)

@orderstring varchar(200), --排序字段(必須!支援多欄位不用加order by)

@wherestring varchar(500) =n'', --條件語句(不用加where)

@pagesize int, --每頁多少條記錄

@pageindex int = 1 , --指定當前為第幾頁

@totalrecord int output --返回總記錄數)as

begin

--處理開始點和結束點

declare @startrecord int;

declare @endrecord int;

declare @totalcountsql nvarchar(500);

declare @sqlstring nvarchar(2000);

set @startrecord = (@pageindex-1)*@pagesize + 1

set @endrecord = @startrecord + @pagesize - 1

set @totalcountsql= n'select @totalrecord = count(*) from ' + @tablename;--總記錄數語句

set @sqlstring = n'(select row_number() over (order by '+ @orderstring +') as rowid,'+@refieldsstr+' from '+ @tablename;--查詢語句

--if (@wherestring! = '' or @wherestring!=null)

begin

set @totalcountsql=@totalcountsql + ' where '+ @wherestring;

set @sqlstring =@sqlstring+ ' where '+ @wherestring;

end--第一次執行得到

--if(@totalrecord is null)

-- begin

exec sp_executesql @totalcountsql,n'@totalrecord int out',@totalrecord output;--返回總記錄數

-- end

----執行主語句

set @sqlstring ='select * from ' + @sqlstring + ') as t where rowid between ' + ltrim(str(@startrecord)) + ' and ' + ltrim(str(@endrecord));

print @sqlstring

exec(@sqlstring)

end

sql 儲存過程分頁

create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...

SQL 儲存過程 分頁

1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...

SQL 分頁儲存過程

create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...