实用的存储过程

记录一下:

查询记录总数:
-- =============================================
-- Author:Dezai
-- Create date:2007-12-27 09:52:33
-- Description:查询字段总记录数集合(通用存储过程)
-- =============================================

create procedure DZ_Count
@Keyfield varchar(30), --主键
@TableName varchar(30),--表名
@strWhere VARCHAR(1500) --查询条件,不包括where字符
as
exec('select count(' + @Keyfield +') from ['+ @TableName +'] Where ' + @strWhere +''  )
go



通用分页:

-- =============================================
-- Author:Dezai
-- Create date:2007-12-27 09:52:33
-- Description:查询分页集合(通用存储过程)
-- =============================================
CREATE PROCEDURE UP_GetCollectionPage
    (
        @tblName VARCHAR(255), -- 表名
        @strGetFields VARCHAR(1000) = '*', -- 需要返回的列
        @fldName VARCHAR(255)='', -- 排序的字段名
        @PageSize INT = 10, -- 页尺寸
        @PageIndex INT = 1, -- 页码
        @doCount BIT = 0, -- 返回记录总数, 非 0 值则返回
        @OrderType BIT = 0, -- 设置排序类型, 非 0 值则降序
        @strWhere VARCHAR(1500) = '' -- 查询条件 (注意: 不要加 WHERE)
    )
AS

    SET NOCOUNT ON

        DECLARE @strSQL VARCHAR(5000) -- 主语句
        DECLARE @strTmp VARCHAR(110) -- 临时变量
        DECLARE @strOrder VARCHAR(400) -- 排序类型


        IF(@doCount != 0)
            BEGIN
                IF @strWhere !=''
                    SET @strSQL = 'SELECT COUNT(1) AS Total FROM [' + @tblName + '] WHERE '+@strWhere
                ELSE
                    SET @strSQL = 'SELECT COUNT(1) AS Total FROM [' + @tblName + ']'
            END
            --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
        ELSE
            BEGIN
                IF @OrderType != 0
                    BEGIN
                        SET @strTmp = '<(SELECT MIN'
                        SET @strOrder = ' ORDER BY [' + @fldName +'] DESC'
                        --如果@OrderType不是0,就执行降序,这句很重要
                    END
                ELSE
                    BEGIN
                        SET @strTmp = '>(SELECT MAX'
                        SET @strOrder = ' ORDER BY [' + @fldName +'] ASC'
                    END

                IF @PageIndex = 1
                    BEGIN
                        IF @strWhere != ''
                            SET @strSQL = 'SELECT TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM [' + @tblName + '] WHERE ' + @strWhere + ' ' + @strOrder
                        ELSE
                            SET @strSQL = 'SELECT TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['+ @tblName + '] '+ @strOrder
                        --如果是第一页就执行以上代码,这样会加快执行速度
                    END
                ELSE
                    BEGIN
                        --以下代码赋予了@strSQL以真正执行的SQL代码
                        SET @strSQL = 'SELECT TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['
                        + @tblName + '] WHERE [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) FROM (SELECT TOP ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] FROM [' + @tblName + ']' + @strOrder + ') AS tblTmp)'+ @strOrder
                        IF @strWhere != ''
                        SET @strSQL = 'SELECT TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['
                        + @tblName + '] WHERE [' + @fldName + ']' + @strTmp + '(['
                        + @fldName + ']) FROM (SELECT TOP ' + str((@PageIndex-1)*@PageSize) + ' ['
                        + @fldName + '] FROM [' + @tblName + '] WHERE ' + @strWhere + ' '
                        + @strOrder + ') AS tblTmp) AND ' + @strWhere + ' ' + @strOrder
                    END
            END

        EXEC (@strSQL)

    SET NOCOUNT OFF
GO



这两个存储过程跟aspnetpager结合使用,会有异想不到的结果.


[本日志由 admin 于 2008-05-10 11:04 PM 编辑]
上一篇: 爱情不是游戏
下一篇: 知识管理推广方案
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 5535
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭