解决方案 »

  1.   

    之所以要@RowCount,是提供给分页控件。
      

  2.   

    呵呵,这个没有办法。
    #1.要想得到TotalCount,必须select count(*) from tablename where 条件
    #2.然后再select fieldlist from tablename where 条件 and rownumber between 1 and 10
    其实楼主可以不必担心效率:因为#1运行时,需要加载相应的数据到内存,#2执行时也需要这一动作(由于分页一般都是先排序,再取其中的某一段数据),#2就可以直接用#1中加载的数据。只要总数据量不是太大,性能还是有保证的。
      

  3.   

    已解决!USE [bjjnkj]
    GO/*
    *名称:分页存储过程返回结果集和记录总数
    *日期:2013年8月2日
    *版本:v1.1
    *描述:1、返回值变量需使用sp_executesql 
    */ALTER PROCEDURE [dbo].[UP_GetRecordByPage]
        @tblName      varchar(255),       -- 表名
        @fldName      varchar(255),       -- 主键字段名
        @PageSize     int = 10,           -- 页尺寸
        @PageIndex    int = 1,            -- 页码
        @IsReCount    bit = 0,            -- 返回记录总数, 非 0 值则返回
        @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
        @strWhere     varchar(1000),   -- 查询条件 (注意: 不要加 where)
        @RecordCount  int=0 output   -- 返回记录数
    AS
    begindeclare @strSQL   varchar(6000)       -- 主语句
    declare @strTmp   varchar(100)        -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
    declare @strOrder varchar(200)        -- 排序类型
    declare @strCount nvarchar(500)   -- 用于返回查询记录数if @OrderType != 0
    begin
        set @strTmp = '<(select min'
        set @strOrder = ' order by [' + @fldName +'] desc'
    end
    else
    begin
        set @strTmp = '>(select max'
        set @strOrder = ' order by [' + @fldName +'] asc'
    end
     
    set @strSQL = 'select top ' + str(@PageSize) + ' * 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) + ' * from ['
            + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
            + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
            + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
            + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
     
    if @PageIndex = 1
    begin
        set @strTmp =''
        if @strWhere != ''
            set @strTmp = ' where ' + @strWhere
     
        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + ']' + @strTmp + ' ' + @strOrder
    endif @IsReCount != 0
        set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
       
    --返回数据集
    exec(@strSQL)declare @TempRecord int
    if @strWhere != ''
    set @strCount='select @a=count(*) from [' + @tblName + ']'+' where ' + @strWhere
    else
    set @strCount='select @a=count(*) from [' + @tblName + ']'--sp_executesql的@statement参数只接受Unicode字符,所以@str需要声明为nvarchar等类型
    exec sp_executesql @strCount,N'@a int output',@TempRecord output
    select @TempRecord
    set @RecordCount=@TempRecord--set @RowCount=@@ROWCOUNTendGO
      

  4.   


    USE [JN_PICS]
    GO/*
    *名称:分页存储过程返回结果集和记录总数,唯一值字段排序。
    *日期:2013年8月2日
    *版本:v1.1,v1.2
    *更新:1、返回值变量需使用sp_executesql 
           2、修复查询总记录2次bug
    */ALTER PROCEDURE [dbo].[sp_get_pagelist2k]
        @tblName      varchar(255),       -- 表名、视图
        @fldName      varchar(255),       -- 主键字段名
        @PageSize     int = 10,           -- 页尺寸
        @PageIndex    int = 1,            -- 页码
        @IsReCount    bit = 0,            -- 返回记录总数, 非 0 值则返回
        @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
        @strWhere     varchar(1000)='',   -- 查询条件 (注意: 不要加 where)
        @RecordCount  int=0 output   -- 返回记录数
    AS
    begindeclare @strSQL   varchar(6000)       -- 主语句
    declare @strTmp   varchar(500)        -- 临时变量(查询条件过长时可能会出错,可修改为1000)
    declare @strOrder varchar(200)        -- 排序类型
    declare @strCount nvarchar(500)   -- 用于返回查询记录数if @OrderType != 0
    begin
        set @strTmp = '<(select min'
        set @strOrder = ' order by [' + @fldName +'] desc'
    end
    else
    begin
        set @strTmp = '>(select max'
        set @strOrder = ' order by [' + @fldName +'] asc'
    end
     
    set @strSQL = 'select top ' + str(@PageSize) + ' * 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) + ' * from ['
            + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
            + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
            + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
            + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
     
    if @PageIndex = 1
    begin
        set @strTmp =''
        if @strWhere != ''
            set @strTmp = ' where ' + @strWhere
     
        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + ']' + @strTmp + ' ' + @strOrder
    end
    --是否返回总记录
    if @IsReCount != 0
    begin
    if @strWhere != ''
    set @strCount='select @RecordCount=count(*) from [' + @tblName + ']'+' where ' + @strWhere
    else
    set @strCount='select @RecordCount=count(*) from [' + @tblName + ']'

    --sp_executesql的@statement参数只接受Unicode字符,所以@str需要声明为nvarchar等类型
    exec sp_executesql @strCount,N'@RecordCount int output',@RecordCount output --返回总记录
    --set @RowCount=@@ROWCOUNT
    end--返回数据集
    exec(@strSQL)endGO上面的存储过程有点问题:
    1、数据集多执行了一次统计记录总数,去掉了。
    2、去掉了临时变量@a等。