分頁procedure (SQL Server)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Paging_Cursor]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Paging_Cursor]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE Paging_Cursor (
@Tables varchar(1000),
@PK varchar(100),
@Sort varchar(200) = NULL,
@PageNumber int = 1,
@PageSize int = 10,
@Fields varchar(1000) = '*',
@Filter varchar(1000) = NULL,
@Group varchar(1000) = NULL)
AS
/* exec Paging_Cursor 'SURVEYINFOTMP','FSFID',null,2 */
/*Find the @PK type*/
DECLARE @PKTable varchar(100)
DECLARE @PKName varchar(100)
DECLARE @type varchar(100)
DECLARE @prec int
IF CHARINDEX('.', @PK) > 0
BEGIN
SET @PKTable = SUBSTRING(@PK, 0, CHARINDEX('.',@PK))
SET @PKName = SUBSTRING(@PK, CHARINDEX('.',@PK) + 1, LEN(@PK))
END
ELSE
BEGIN
SET @PKTable = @Tables
SET @PKName = @PK
END
SELECT @type=t.name, @prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
WHERE o.name = @PKTable AND c.name = @PKName
IF CHARINDEX('char', @type) > 0
SET @type = @type + '(' + CAST(@prec AS varchar) + ')'
DECLARE @strPageSize varchar(50)
DECLARE @strStartRow varchar(50)
DECLARE @strFilter varchar(1000)
DECLARE @strGroup varchar(1000)
/*Default Sorting*/
IF @Sort IS NULL OR @Sort = ''
SET @Sort = @PK
/*Default Page Number*/
IF @PageNumber < 1
SET @PageNumber = 1
/*Set paging variables.*/
SET @strPageSize = CAST(@PageSize AS varchar(50))
SET @strStartRow = CAST(((@PageNumber - 1)*@PageSize + 1) AS varchar(50))
/*Set filter & group variables.*/
IF @Filter IS NOT NULL AND @Filter != ''
SET @strFilter = ' WHERE ' + @Filter + ' '
ELSE
SET @strFilter = ''
IF @Group IS NOT NULL AND @Group != ''
SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
SET @strGroup = ''
/*Execute dynamic query*/
EXEC(
'DECLARE @PageSize int
SET @PageSize = ' + @strPageSize + '
DECLARE @PK ' + @type + '
DECLARE @tblPK TABLE (
PK ' + @type + ' NOT NULL PRIMARY KEY
)
DECLARE PagingCursor CURSOR DYNAMIC READ_ONLY FOR
SELECT ' + @PK + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
OPEN PagingCursor
FETCH RELATIVE ' + @strStartRow + ' FROM PagingCursor INTO @PK
SET NOCOUNT ON
WHILE @PageSize > 0 AND @@FETCH_STATUS = 0
BEGIN
INSERT @tblPK (PK) VALUES (@PK)
FETCH NEXT FROM PagingCursor INTO @PK
SET @PageSize = @PageSize - 1
END
CLOSE PagingCursor
DEALLOCATE PagingCursor
SELECT ' + @Fields + ' FROM ' + @Tables + ' JOIN @tblPK tblPK ON ' + @PK + ' = tblPK.PK ' + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort
)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7718881/viewspace-1035454/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL SERVER分頁演算法SQLServer演算法
- [SQL Server]分頁功能的實現SQLServer
- MySql/Oracle和SQL Server的分頁查MySqlOracleServer
- Sql Server系列:查詢分頁語句SQLServer
- (SQL Server)分頁的儲存過程SQLServer儲存過程
- 分頁語法 FOR SQL SERVER 2012SQLServer
- SQL Server的分頁優化及Row_Number()分頁存在的問題SQLServer優化
- SQL SERVER 2005分頁儲存過程SQLServer儲存過程
- MySQL、Oracle和SQL Server的分頁查詢語句MySqlOracleServer
- SQL Server 2005快速Web分頁的實現SQLServerWeb
- SQL Server 儲存過程的經典分頁(轉)SQLServer儲存過程
- Sql Server 2005 row_number()分頁效能測試SQLServer
- sql primary key procedureSQL
- SQL Server 2000 的分頁查詢(儲存過程)SQLServer儲存過程
- SQL資料分頁SQL
- SQL分頁語句SQL
- Linq to sql 分頁SQL
- EXECUTE IMMEDIATE dynamic sql in procedureSQL
- SQL SERVER中的頁和區SQLServer
- 基於Sql server資料庫的四種分頁方式總結SQLServer資料庫
- asp.net SQL Server 儲存過程分頁及程式碼呼叫ASP.NETSQLServer儲存過程
- 通用的SQL Server資料庫查詢分頁儲存過程SQLServer資料庫儲存過程
- SQL Server中利用SET ROWCOUNT來高效能地進行分頁SQLServer
- SQL Server中對比表數量,索引數量及procedure數量SQLServer索引
- 三種SQL分頁方式SQL
- sql server分組查詢示例SQLServer
- 簡單談基於SQL SERVER 分頁儲存過程的演進SQLServer儲存過程
- SQL Server-儲存過程(Procedure),帶入引數和出引數SQLServer儲存過程
- SQL Server AlwaysOn讀寫分離配置SQLServer
- mybatis動態sql與分頁MyBatisSQL
- SQL 分頁儲存過程SQL儲存過程
- SQL - 常用資料庫分頁SQL資料庫
- Mysql 分頁效率不同的SQLMySql
- oracle sql分頁查詢(一)OracleSQL
- sql儲存過程分頁SQL儲存過程
- SQL Server 資料頁損壞修復SQLServer
- SQL 建立儲存過程PROCEDURESQL儲存過程
- 使用SQL Server 2005的新函式構造分頁儲存過程SQLServer函式儲存過程