精彩的近乎完美的分页存储过程

2016-01-29 16:24 7 1 收藏

精彩的近乎完美的分页存储过程,精彩的近乎完美的分页存储过程

【 tulaoshi.com - SQLServer 】

 

CREATE procedure main_table_pwqzc
(@pagesize int,
@pageindex int,
@docount bit,
@this_id)
as
if(@docount=1)
begin
select count(id) from luntan where this_id=@this_id
end
else
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select id from luntan where this_id=@this_id order by reply_time desc
select a.* from luntan a,@indextable t where a.id=t.nid
and t.id@PageLowerBound and t.id<=@PageUpperBound order by t.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
而这句也只从表中取出所需要的记录,而不是所有的记录,结合起来,极大的提高了效率!!
妙啊,真的妙!!!!


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) + ')'

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/sqlserver/)

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))

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/sqlserver/)

/*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 RO

来源:http://www.tulaoshi.com/n/20160129/1498518.html

延伸阅读
标签: ASP
  /*****听以前的同事说asp页面上的分页太慢了(如果数据多了), 就想了这么个笨办法。有些地方还要考虑----比如select top 22 * from cat_list where T_id not in (select T_id from #change)是否有效率问题;数据不能重复等等 不过灵活性挺好。希望各位高手再给帮忙改正;多谢chair3的帮助---这个存储过程还可以在加入几个变量,随便...
代码如下: create database Test on primary ( name='Test_Data.mdf', filename='D:\我的资料\sql\备份\Test_Data.mdf' ) log on ( name='Test_Data.ldf', filename='D:\我的资料\sql\备份\Test_Data.ldf' ) if object_id('tb') is not null drop table tb create table tb ( Col int ) insert into tb select top 50 number ...
标签: SQLServer
建立表: CREATE TABLE [TestTable] (  [ID] [int] IDENTITY (1, 1) NOT NULL ,  [FirstName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,  [LastName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,  [Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,  [Note] [nvarchar] (2000) COLLATE C...
标签: ASP
       我的一个asp + sqlserver的分页程序:这个程序的特点就是指将当前页的纪录掉到页面上来。性能不错,而且不用存储过程,由于代码没有封装,所以很零散,要用到你的程序中,需要先简单读读。然后注意需要自己设置的地方的意思。有些是可以省略的。有些参数传递也是可以省略的。     &n...
标签: ASP
一、创建表 tiku_koushi if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tiku_koushi]') and OBJECTPROPERTY (id, N'IsUserTable') = 1) drop table [dbo].[tiku_koushi] GO CREATE TABLE [dbo].[tiku_koushi] ( [id] [int] IDENTITY (1, 1) NOT NULL , [title] [varchar] (250) COLLATE Chinese_PRC_CI_AS N...

经验教程

514

收藏

32
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部