ALTER PROCEDURE [dbo].[P_Common_Pagination]
(
@currentPage int,
@pageSize int,
@querySQL nvarchar(max),
@orderSQL nvarchar(max),
@totalSQL nvarchar(max),
@totalCount int output,
@newCurrentPage int output,
@exceptionMsg nvarchar(4000) output
)
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
END
ELSE
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),
@totalCount int output,
@newCurrentPage int output,
@exceptionMsg nvarchar(4000) output
)
AS
DECLARE @beforeSQL nvarchar(max)
DECLARE @midSQL nvarchar(max)
DECLARE @querySQL nvarchar(max)
DECLARE @orderSQL nvarchar(max)
DECLARE @totalSQL nvarchar(max)
DECLARE @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'
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)) + '''';
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)