存储过程
USE [Comom]
GO
/****** Object: StoredProcedure [dbo].[Common_PageList] Script Date: 03/26/2019 21:27:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Common_PageList]
(
@tab nvarchar(200),---表名
@strFld nvarchar(max), --字段字符串
@strWhere nvarchar(max), --where条件
@PageIndex int, --页码
@PageSize int, --每页容纳的记录数
@Sort nvarchar(255), --排序字段及规则,不用加order by
@Total int output --总数
)
AS
declare @strSql nvarchar(max)
set nocount on;
set @strSql=' SELECT * FROM (SELECT ROW_NUMBER()
OVER(ORDER BY ' + @Sort + ') AS rownum, ' + @strFld + ' FROM ' + @tab + ' where 1=1 ' + @strWhere + ') AS Dwhere
WHERE rownum BETWEEN ' + CAST(((@PageIndex-1)*@PageSize + 1) as nvarchar(20)) + ' and ' + cast((@PageIndex*@PageSize) as nvarchar(20))
exec (@strSql)
--print @strSql
set @strSql='SELECT @Total = COUNT(0) FROM ' + @tab + ' WHERE 1=1 ' + @strWhere
--print @strSql
EXECUTE sp_executesql @strSql,N'@Total INT OUTPUT',@Total OUTPUT
set nocount off;
------------------------------------------------------------------------------------------------------------
以下调用
exec Common_PageList 'TableA','*',' and condition = '第一次' ',1,10,'ddate desc',0;
慕设计2395807
相关分类