CREATE PROC Turnpage
@qCols varchar(200), --需要查詢的列
@qTables varchar(200), --需要查詢的表 和條件
@iKey varchar (20), --標識字段
@oKey varchar(20), --排序字段
@pageSize int, --每頁的行數
@pageNumber int --要顯示的頁碼, 從0開始
AS
set nocount on
BEGIN
DECLARE @sqlText AS varchar(1000)
DECLARE @sqlTable AS varchar(1000)
SET @sqlTable = 'SELECT TOP ' + CAST((@pageNumber + 1) * @pageSize AS varchar(30)) + ' ' + @qCols +' from '+ @qTables + ' order by '+@oKey+ ' desc'
SET @sqlText =
'SELECT TOP ' + CAST(@pageSize AS varchar(30)) + ' * ' +
'FROM (' + @sqlTable + ') AS tableA ' +
'WHERE ' + @iKey + ' NOT IN(SELECT TOP ' +
CAST(@pageNumber * @pageSize AS varchar(30)) + ' ' + @iKey +
' FROM (' + @sqlTable + ') AS tableB)'
EXEC (@sqlText)
--print(@sqltext)
END
GO