標籤:des style color ar 使用 sp 資料 on bs
-- 使用方法:在擷取查詢列表的預存程序中執行,並指定參數就行了
-- exec ListPage @SQL, @PageSize, @PageNo, @OrderStr, @OrderType
-- @SQL 查詢語句
-- @PageSize 資料數量
-- @PageNo 當前頁碼
-- @OrderStr 排序欄位
-- @OrderType 排序 0 ASC 1 DESC
USE [DataBaseName]--資料庫名稱
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE ListPage
@InSQL varchar(max),
@RowNum int,
@PageNo int,
@IndexWord varchar(200),
@Order bit=0
AS
DECLARE @SQL varchar(max)
DECLARE @OrderMode varchar(10)
DECLARE @BeginRow int
DECLARE @EndRow int
BEGIN
IF @Order = 0
BEGIN
SET @OrderMode = ‘ASC‘
END
ELSE
BEGIN
SET @OrderMode = ‘DESC‘
END
IF @PageNo = 1
BEGIN
SET @BeginRow = 1
SET @EndRow = @RowNum
END
ELSE
BEGIN
SET @BeginRow = (@RowNum * (@PageNo-1))+1
SET @EndRow = @BeginRow + @RowNum - 1
END
SET @sql = ‘SELECT *,ROW_NUMBER() OVER(ORDER BY ‘+ @IndexWord +‘ ‘+ @OrderMode +‘) AS ROWNUMBER FROM (‘+ @InSQL +‘) AS A‘
SET @sql = ‘select * from (‘+ @sql +‘) as b where b.ROWNUMBER BETWEEN ‘+ convert(varchar(10),@BeginRow) +‘ AND ‘+ convert(varchar(10),@EndRow) +‘‘
Exec (@sql)
END
GO
SQL Server 公用分頁預存程序及使用方法