多表通用的SQL存儲過程分頁 USE [EmailCenter] GO /****** Object: StoredProcedure [dbo].[Common_PageList] Script Date: 2016/2/29 11:00:19 ******/ SET ANSI_NULLS ON
多表通用的SQL存儲過程分頁
USE [EmailCenter] GO /****** Object: StoredProcedure [dbo].[Common_PageList] Script Date: 2016/2/29 11:00:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROC [dbo].[Common_PageList] ( @tab nvarchar(200),---表名 @strFld nvarchar(max), --欄位字元串 @strWhere varchar(max), --where條件 @PageIndex int, --頁碼 @PageSize int, --每頁容納的記錄數 @Sort VARCHAR(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) set @strSql='SELECT @Total = COUNT(0) FROM ' + @tab + ' WHERE 1=1 ' + @strWhere EXECUTE sp_executesql @strSql,N'@Total INT OUTPUT',@Total OUTPUT set nocount off; GO