(SQL Server)分頁的儲存過程
http://www.cnitblog.com/ffan/archive/2005/07/30/1194.html
尋夢的歲月不言辛苦幾許,不問收穫幾多…
(SQL Server)分頁的儲存過程
CREATE procedure main_table_pwqzc
(@pagesize int,
@pageindex int,
@docount bit,
@this_id int)
as
if(@docount=1)
begin
select count(id) from luntan where this_id=@this_id
end
else
begin
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
create table #pageindex(id int identity(1,1) not null,nid int)
set rowcount @PageUpperBound
insert into #pageindex(nid)
select id from luntan where this_id=@this_id order by reply_time desc
select O.*
from luntan O,#pageindex p
where O.id=p.nid and p.id>@PageLowerBound and p.id<=@PageUpperBound order by p.id
end
GO
(@pagesize int,
@pageindex int,
@docount bit,
@this_id int)
as
if(@docount=1)
begin
select count(id) from luntan where this_id=@this_id
end
else
begin
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
create table #pageindex(id int identity(1,1) not null,nid int)
set rowcount @PageUpperBound
insert into #pageindex(nid)
select id from luntan where this_id=@this_id order by reply_time desc
select O.*
from luntan O,#pageindex p
where O.id=p.nid and p.id>@PageLowerBound and p.id<=@PageUpperBound order by p.id
end
GO
儲存過程會根據傳入的引數@docount來確定是不是要返回所有要分頁的記錄總數
特別是這兩行
set rowcount @PageUpperBound
insert into @indextable(nid) select id from luntan where
this_id=@this_id order by reply_time desc
真的是妙不可言!!set rowcount @PageUpperBound當記錄數達到@PageUpperBound時就會停止處理查詢
,select id 只把id列取出放到臨時表裡,select a.* from luntan a,@indextable t where a.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
而這句也只從表中取出所需要的記錄,而不是所有的記錄,結合起來,極大的提高了效率!!
(2)
CREATE PROCEDURE Paging_RowCount
(
@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
/*Default Sorting*/
IF @Sort IS NULL OR @Sort = ''
SET @Sort = @PK
/*Find the @PK type*/
DECLARE @SortTable varchar(100)
DECLARE @SortName varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int
/*Set sorting variables.*/
IF CHARINDEX('DESC',@Sort)>0
BEGIN
SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
SET @operator = '<='
END
ELSE
BEGIN
IF CHARINDEX('ASC', @Sort) = 0
SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
SET @operator = '>='
END
IF CHARINDEX('.', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET @SortTable = @Tables
SET @SortName = @strSortColumn
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 = @SortTable AND c.name = @SortName
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 @strSimpleFilter varchar(1000)
DECLARE @strGroup varchar(1000)
/*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 != ''
BEGIN
SET @strFilter = ' WHERE ' + @Filter + ' '
SET @strSimpleFilter = ' AND ' + @Filter + ' '
END
ELSE
BEGIN
SET @strSimpleFilter = ''
SET @strFilter = ''
END
IF @Group IS NOT NULL AND @Group != ''
SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
SET @strGroup = ''
/*Execute dynamic query*/
EXEC(
'
DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
'
)
GO
(
@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
/*Default Sorting*/
IF @Sort IS NULL OR @Sort = ''
SET @Sort = @PK
/*Find the @PK type*/
DECLARE @SortTable varchar(100)
DECLARE @SortName varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int
/*Set sorting variables.*/
IF CHARINDEX('DESC',@Sort)>0
BEGIN
SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
SET @operator = '<='
END
ELSE
BEGIN
IF CHARINDEX('ASC', @Sort) = 0
SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
SET @operator = '>='
END
IF CHARINDEX('.', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET @SortTable = @Tables
SET @SortName = @strSortColumn
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 = @SortTable AND c.name = @SortName
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 @strSimpleFilter varchar(1000)
DECLARE @strGroup varchar(1000)
/*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 != ''
BEGIN
SET @strFilter = ' WHERE ' + @Filter + ' '
SET @strSimpleFilter = ' AND ' + @Filter + ' '
END
ELSE
BEGIN
SET @strSimpleFilter = ''
SET @strFilter = ''
END
IF @Group IS NOT NULL AND @Group != ''
SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
SET @strGroup = ''
/*Execute dynamic query*/
EXEC(
'
DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
'
)
GO
相關文章
- SQL Server 儲存過程的經典分頁(轉)SQLServer儲存過程
- SQL SERVER 2005分頁儲存過程SQLServer儲存過程
- SQL 分頁儲存過程SQL儲存過程
- sql儲存過程分頁SQL儲存過程
- SQL Server 2000 的分頁查詢(儲存過程)SQLServer儲存過程
- Sql儲存過程分頁--臨時表儲存SQL儲存過程
- 通用的SQL Server資料庫查詢分頁儲存過程SQLServer資料庫儲存過程
- SQL Server 儲存過程SQLServer儲存過程
- 【SQL Server】--儲存過程SQLServer儲存過程
- 兩種SQL分頁方法儲存過程和遊標儲存過程SQL儲存過程
- 簡單談基於SQL SERVER 分頁儲存過程的演進SQLServer儲存過程
- 分頁儲存過程儲存過程
- asp.net SQL Server 儲存過程分頁及程式碼呼叫ASP.NETSQLServer儲存過程
- MS SQL Server儲存過程SQLServer儲存過程
- Sql Server系列:儲存過程SQLServer儲存過程
- 解密SQL SERVER儲存過程解密SQLServer儲存過程
- 通用分頁儲存過程儲存過程
- SQL Server 儲存過程的運用SQLServer儲存過程
- 使用SQL Server 2005的新函式構造分頁儲存過程SQLServer函式儲存過程
- SQL Server基礎:儲存過程SQLServer儲存過程
- sql server儲存過程語法SQLServer儲存過程
- SQL server儲存過程函式SQLServer儲存過程函式
- 簡單的儲存過程分頁儲存過程
- 簡單的分頁儲存過程儲存過程
- SqlServer-儲存過程分頁SQLServer儲存過程
- 使用SQL Server2005的新函式構造分頁儲存過程SQLServer函式儲存過程
- SQL Server儲存過程中raiserror的使用SQLServer儲存過程AIError
- SQL Server儲存過程的優缺點SQLServer儲存過程
- asp.net分頁的SQL語句及儲存過程ASP.NETSQL儲存過程
- SQL Server基礎之儲存過程SQLServer儲存過程
- 基於ROWCOUNT的分頁儲存過程儲存過程
- oracle儲存過程分頁程式碼Oracle儲存過程
- Oracle 儲存過程分頁 + Sqlsugar呼叫Oracle儲存過程SqlSugar
- SQL SERVER儲存過程AS和GO的含義SQLServer儲存過程Go
- Java呼叫SQL Server的儲存過程詳解JavaSQLServer儲存過程
- 支援DISTINCT的通用分頁儲存過程(SQL2005)儲存過程SQL
- SQL Server 禁用擴充套件儲存過程SQLServer套件儲存過程
- SQL Server 資料備份儲存過程SQLServer儲存過程