alter procedure [dbo].[p_common_pagination]
( @currentpage int, --當前頁碼
@pagesize int, --每頁記錄數
@querysql nvarchar(max), --查詢sql
@ordersql nvarchar(max), --排序sql
@totalsql nvarchar(max), --查詢總記錄數sql
@totalcount int output, --總記錄數
@newcurrentpage int output, --返回的當前頁碼
@exceptionmsg nvarchar(4000) output --異常資訊,無異常返回的是null,有異常返回具體的異常資訊)as
declare @startindex int
declare @endindex int
declare @maxpage int
begin
begin try -- 異常捕獲
set @totalsql = n'select @totalcount=' + @totalsql;
--執行
exec sp_executesql @totalsql,n'@totalcount int output',@totalcount output
--判斷當前頁是否超出總頁
if 0 != @totalcount
begin
set @maxpage = (@totalcount / @pagesize);
if 0 != (@totalcount % @pagesize)
set @maxpage = @maxpage + 1
if @currentpage > @maxpage
set @currentpage = @maxpage
endelse
set @currentpage = 1
set @newcurrentpage = @currentpage
set @startindex = (@currentpage - 1)*@pagesize + 1;
set @endindex = @currentpage*@pagesize;
set @querysql = n'select *
from (
select row_number() over (' + @ordersql + ') as rownum,'
+ @querysql
+ ') tt where rownum between ' + cast(@startindex as varchar(50))
+ ' and ' + cast(@endindex as varchar(50));
exec (@querysql)
end try
begin catch --異常發生後,相應的事務等處理
--異常資訊
set @exceptionmsg = '訊息 ' + cast(error_number() as varchar(50)) + ',級別 '
+ cast(error_severity() as varchar(50)) + ',狀態 '
+ cast(error_state() as varchar(50)) + ',過程 ' + 'p_common_pagination'
+ ',第 ' + cast(error_line() as varchar(50)) + ' 行'
+ ':' + error_message();
insert into logger(logtype, information)
values('error', @exceptionmsg);
end catch
end
分頁查詢例項
alter procedure [dbo].[p_exceptionwaybillpagelist]
(@currentpage int, --當前頁碼
@pagesize int, --每頁記錄數
@conditionxml nvarchar(max), --條件引數xml字串
@totalcount int output, --總記錄數
@newcurrentpage int output, --返回的當前頁碼
@exceptionmsg nvarchar(4000) output --異常資訊,無異常返回的是null,有異常返回具體的異常資訊)as
declare @beforesql nvarchar(max) --查詢sql的頭部
declare @midsql nvarchar(max) --中間公用的sql
declare @querysql nvarchar(max) --查詢sql(去掉頭部的select和尾部的排序sql)
declare @ordersql nvarchar(max) --尾部的排序sql
declare @totalsql nvarchar(max) --查詢總記錄數sql(去掉頭部的select)
declare @xml xml --查詢條件的xml
declare @outsidnvarchar(100)
declare @begintime nvarchar(100)
declare @endtime nvarchar(100)
begin
begin try -- 異常捕獲
--必須放在最前面,緊接著事務點
set @xml = substring(@conditionxml,39,len(@conditionxml)-38);
select @outsid = c.doc.value('outsid[1]','varchar(100)'),
@begintime = c.doc.value('begintime[1]','varchar(100)'),
@endtime = c.doc.value('endtime[1]','varchar(100)')
from @xml.nodes('/nodes/node') c(doc);
set @beforesql = 'wb.out_sid, wb.company_name, wb.deliver_time'
set @midsql = ' from waybill wb where 1 = 1'
--************************************ 查詢條件處理start ************************************--
if isnull(@outsid, '') <> ''
set @midsql = @midsql + ' and wb.out_sid = ''' + @outsid + '''';
if isnull(@begintime, '') <> ''
set @midsql = @midsql + ' and wb.deliver_time >= ''' + convert(varchar, cast(@begintime as datetime), (23)) + '''';
if isnull(@endtime, '') <> ''
set @midsql = @midsql + ' and wb.deliver_time < ''' + convert(varchar, cast(@endtime as datetime) + 1, (23)) + '''';
--************************************ 查詢條件處理end ************************************--
set @ordersql = ' order by wb.deliver_time'
set @querysql = @beforesql + @midsql
set @totalsql = 'count(*)' + @midsql
exec p_common_pagination @currentpage, @pagesize, @querysql, @ordersql, @totalsql, @totalcount output, @newcurrentpage output, @exceptionmsg output;
end try
begin catch --異常發生後,相應的事務等處理
--異常資訊
set @exceptionmsg = '訊息 ' + cast(error_number() as varchar(50)) + ',級別 '
+ cast(error_severity() as varchar(50)) + ',狀態 '
+ cast(error_state() as varchar(50)) + ',過程 ' + 'p_exceptionwaybillpagelist'
+ ',第 ' + cast(error_line() as varchar(50)) + ' 行'
+ ':' + error_message();
--異常日誌
insert into logger(logtype, information)
values('error', @exceptionmsg);
end catch
end
SQL SERVER分頁儲存過程
使用儲存過程寫乙個分頁查詢 select from t users gocreate proc usp getdatafy pagesize int 10,每頁記錄條數 定義變數並賦初始值 pageindex int 1,當前要檢視第幾頁的記錄 sumcount int output,總的記錄條數 ...
SqlServer分頁儲存過程
高效分頁語句 1 row number select from select row number over order byrpid as rowfrom ou rolepermission as twheret.row 0 andt.row 10 2,top 分頁查詢 select top 10...
sql server 分頁儲存過程
sql server 分頁儲存過程,在網上找了很多,但是都不能用或者不能很好的用,特別綜合大家的智慧型,自己寫了乙個,而且通過visual studio除錯,通過實際資料測試 set ansi nulls on set quoted identifier on go alter procedure ...