类别:数据库
日期:2020-02-29 浏览:1974 评论:0
SqlServer分页存储过程:
USE [SysFrame] GO /****** Object: StoredProcedure [dbo].[usp_DataPager] Script Date: 01/16/2020 15:02:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[usp_DataPager] ( @viewName VARCHAR(2000), --Table or View Name @fieldName VARCHAR(800) = '*', --Query column @pageSize INT = 20, --Pagesize @pageNo INT =1, --current page @orderString VARCHAR(2000), --order by @whereString VARCHAR(MAX) = '1=1', --WHERE @recordTotal INT OUTPUT --total count )AS BEGIN DECLARE @beginRow INT DECLARE @endRow INT DECLARE @tempLimit VARCHAR(MAX) DECLARE @tempCount NVARCHAR(MAX) DECLARE @tempMain VARCHAR(MAX) SET @beginRow = (@pageNo - 1) * @pageSize+ 1 SET @endRow = @pageNo * @pageSize SET @tempLimit = 'rows BETWEEN ' + CAST(@beginRow AS VARCHAR) +' AND '+CAST(@endRow AS VARCHAR) --return total count SET @tempCount = 'SELECT @recordTotal = COUNT(*) FROM (SELECT 1 as a FROM '+@viewName+' WHERE '+@whereString+') As my_temp' EXECUTE sp_executesql @tempCount,N'@recordTotal INT OUTPUT',@recordTotal OUTPUT --return query data SET @tempMain = 'SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY '+@orderString+') AS rows ,'+@fieldName+' FROM '+@viewName+' WHERE '+@whereString+') AS main_temp WHERE '+@tempLimit EXECUTE (@tempMain) END
本文标题:SqlServer:分页的存储过程 ~~
本文链接:https://vtzw.com/post/61.html
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
发表评论 / 取消回复