以前写的一个分页存储过程刚才不小心翻出来的

2016-02-19 12:09 5 1 收藏

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享以前写的一个分页存储过程刚才不小心翻出来的,希望可以对大家能有小小的帮助。

【 tulaoshi.com - Web开发 】

CREATE PROCEDURE GoalerPageSp
@IntPageSize int,
@IntCurrPage int,
@strFields nvarchar(2000),
@strTable varchar(200),
@strWhere varchar(800),
@strOrderType varchar(200),
@strKeyField varchar(50)
AS
SET NOCOUNT ON
DECLARE @tmpSQL nvarchar(4000)--存放动态SQL语句
DECLARE @tmpWhere varchar(800)
DECLARE @tmpAndWhere varchar(800)--用于第N(1)页上边的查询条件
DECLARE @tmpOrder varchar(200)
DECLARE @tmpD_X varchar(2)
DECLARE @tmpMin_MAX varchar(3)

--设置条件--
IF @strWhere IS NULL OR RTRIM(@strWhere)=''
    BEGIN --没有查询条件
        SET @tmpWhere=''
        SET @tmpAndWhere=''
    END 
ELSE 
    BEGIN --有查询条件
        SET @tmpWhere=' WHERE '+@strWhere
        SET @tmpAndWhere=' AND '+@strWhere
    END 

--设置排序--
IF @strOrderType != 0
    BEGIN--倒序 
        SET @tmpD_X = ''
        SET @tmpMin_MAX = 'MIN'
        SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' DESC'
    END 
ELSE 
    BEGIN 
        SET @tmpD_X = ''
        SET @tmpMin_MAX = 'MAX'
        SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' ASC'
    END 
--SQL查询--
IF @IntCurrPage=1
    Set @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder
ELSE
    SET @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (SELECT '+@tmpMin_MAX+'('+@strKeyField+') FROM (SELECT TOP '+CAST(@IntPageSize*(@IntCurrPage-1) AS VARCHAR)+' '+@strKeyField+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') AS T))'+@tmpAndWhere+' '+@tmpOrder
EXEC(@tmpSQL)
GO

调用方法:
IntPageSize=20
strTable=" [TableName] "    '数据表名称
strFields=" Field1,Field2,Field3,Field4 "    '需要读取的列名
strKeyField="Field1"    '主键:这里假设Field1为主键
strWhere=""    '条件:FieldA='b'
strOrderType=1    '排序方式:1为倒序,0为顺序

CurrPage=Request.QueryString("Page")
IF(CurrPage"" And Isnumeric(CurrPage))THEN
    CurrPage=CLNG(CurrPage)   
    IF(CurrPage1)THEN CurrPage=1                   
ELSE
    CurrPage=1
END IF

IF strWhere"" THEN
    tmpWhere=" WHERE "&strWhere
ELSE 
    tmpWhere=""
END IF

IF(SESSION("RecCount")"")THEN
    IF(SESSION("strWhere")strWhere)THEN
        RecCount=Conn.Execute("SELECT COUNT("&strKeyField&") FROM "&strTable&tmpWhere)(0)
        SESSION("RecCount")=RecCount
        SESSION("strWhere")=strWhere
    ELSE
        RecCount=SESSION("RecCount")
    END IF
ELSE
    RecCount=Conn.Execute("SELECT COUNT(*) FROM "&strTable&tmpWhere)(0)
    SESSION("RecCount")=RecCount
    SESSION("strWhere")=strWhere
END IF

IF(RecCount MOD IntPageSize 0)THEN
    IntPageCount=INT(RecCount/IntPageSize)+1
ELSE
    IntPageCount=RecCount/IntPageSize
END IF

SET Cmd=Server.CreateObject("Adodb.Command") 
Cmd.CommandType=4 
SET Cmd.ActiveConnection=Conn 
Cmd.CommandText="GoalerPageSp" 
Cmd.Parameters.Append Cmd.CreateParameter("@IntPageSize",4,1,4,IntPageSize)
Cmd.Parameters.Append Cmd.CreateParameter("@IntCurrPage",4,1,4,CurrPage)
Cmd.Parameters.Append Cmd.CreateParameter("@strFields",200,1,2000,strFields)
Cmd.Parameters.Append Cmd.CreateParameter("@strTable",200,1,200,strTable)
Cmd.Parameters.Append Cmd.CreateParameter("@strWhere",200,1,800,strWhere)
Cmd.Parameters.Append Cmd.CreateParameter("@strOrderType",4,1,4,strOrderType)
Cmd.Parameters.Append Cmd.CreateParameter("@strKeyField",200,1,50,strKeyField)
SET RS=Cmd.Execute()
IF RecCount1 THEN
    Response.Write("没有记录")
ELSE
    GetRecord=RS.GetRows(IntPageSize)
    For i=0 To Ubound(GetRecord,2)
        Response.Write(GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))    '...输出内容
    NEXT
    GetRecord=Null
END IF
SET RS=NOTHING

有用的朋友请自己慢慢调试吧,总记录是用ASP来取的,存储在SESSION里边,如果每次都统计一次总记录,将会非常费时,当然,如果你想在存储过程里来取总记录和总页数然后返回也是可以的,下边是代码:
--获取记录总数--
SET @tmpSQL='SELECT @getRecordCounts=COUNT('+@strKeyField+') FROM '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts int output',@getRecordCounts OUTPUT

--获取总页数--
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
IF @getRecordCounts=@IntPageSize
    SET @getPageCounts=1
ELSE
BEGIN
    IF @tempFolatNumber != 0
        SET @getPageCounts=(@getRecordCounts/@IntPageSize)+1
    ELSE
        SET @getPageCounts=(@getRecordCounts/@IntPageSize)
END

别忘了返回定义参数:
@getRecordCounts int output,--返回总记录
@getPageCounts int output--返回总页数

来源:https://www.tulaoshi.com/n/20160219/1599977.html

延伸阅读
标签: ASP
  if exists(select * from sysobjects where ID = object_id("up_TopicList"))    drop proc up_TopicList go create proc up_TopicList             @a_ForumID int , @a_intDays int , @a_intPageNo int , @a_intPageSize tinyint  &nbs...
经验分享 图钉问: 以前不小心感染念珠菌,后来治好了,有一点点阴道炎,但是最后还是好了,只是昨天看到疑似以前念珠菌那样的分泌物,请问是病复发了吗? 图老师答: 阴道炎是妇女常见的妇科疾病,容易复发,可能是复发了。建议用温水清洗勤换内裤观察观察。
其实在很多时候设计的度还是要把握的,不至于让自己陷入的怪圈中才是最重要的,因为我们还要留出时间还解决其他的很多问题,个人认为适度就可以了,留出一定的空间。也因为万能是不存在的,万物在一定的范畴之内都是合理的,出了范畴可能就没有合理的了。          分页存储过程大致有下列几种 1、&n...
过期妊娠对母婴都有不利 什么叫过期Tulaoshi.com妊娠呢?过期妊娠,对孕妇和胎儿会有伤害吗?正常情况下,胎儿从受孕、生长发育到娩出约需280天,即40周左右。 过期妊娠对母婴都有害 丽丽妊娠已超过预产期,至今小家伙仍“按兵不动”。别人都很着急,可她婆母却说,女孩懒月,到时生的一定是女孩,要媳妇在家等...
代码如下: /* *@curentpage 当前页 *@pagesize 每页记录数 *@TableName 表名 *@key 主键(自动排序) *@where 查询条件 1)空为 null 2)有查询条件不要带where *@order '0'表示 desc '1'是asc *@pageCount 总页数 */ create procedure Page @currentpage int,@pagesize int, @TableName varchar(30),@key varchar(30), @where...

经验教程

608

收藏

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