CREATE PROCEDURE p_QueryByPage_FinanceHistory 
  @tblName varchar(255), -- 表名 
  @strGetFields varchar(1000) = '*', -- 需要返回的列 
  @fldName varchar(255)='', -- 排序的字段名 
  @PageSize int = 10, -- 页尺寸 
  @StartPageIndex int = 1, -- 起始行数 
  @doCount bit = 0, -- 返回记录总数, 非 0 值则返回 
  @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 
  @strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where) 
  AS 
  declare @strInsertSQL varchar(5000) --插入到新表语句 
  declare @strSQL  varchar(5000) --sql语句 
  declare @strOrder varchar(400) -- 排序类型 
  declare @PageIndexEnd int --结束行数 
  set nocount on
  if @doCount != 0 
  begin 
  if @strWhere !='' 
  set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere 
  else 
  set @strSQL = 'select count(*) as Total from ' + @tblName 
  exec (@strSQL) 
  --print @strSQL 
  end 
  --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况 
  else 
  begin 
        set @PageIndexEnd = @StartPageIndex + @PageSize 
  if @OrderType != 0 
  begin 
  set @strOrder = ' order by ' + @fldName +' desc' 
  --如果@OrderType不是0,就执行降序,这句很重要! 
  end 
  else 
  begin 
  set @strOrder = ' order by ' + @fldName +' asc' 
end   begin 
  --创建临时表 
CREATE TABLE #TempTblFinanceHistory ( 
temp_id int IDENTITY(1,1) PRIMARY KEY,ID int, 
                Stock varchar(8),FQuarter int,ModifyDate int,ZGB float,LTGA float,LTGB float, 
        GDZS float,SYL float,MGYL float,MGJZC float,ZYLRL float,ZYZZL float,ZZCSYL float, 
                ZCFZBL float,ZYYWSR float,ZCZJ float,FZZJ float,GDQYHJ float,GJJ float,ZYYWLR float, 
                JLR float,TZSY float) 
                --插入数据 
  if @strWhere != '' 
  begin 
      set @strInsertSQL = 'INSERT INTO #TempTblFinanceHistory 
                      SELECT ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL,MGJZC, 
                              ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,ZYYWLR,JLR,TZSY  
                              from ['+@tblName+']where '+ @strWhere 
                + @strOrder 
  end 
  else 
  begin 
          set @strInsertSQL = 'INSERT INTO #TempTblFinanceHistory 
                      SELECT ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL, 
                              MGJZC,ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ, 
                              ZYYWLR,JLR,TZSY from ['+ @tblName +']'+ @strOrder  
  end 
  exec (@strInsertSQL)  
                --查询 
                set nocount oFF
                select temp_id,ID,Stock,FQuarter,ModifyDate,ZGB,LTGA,LTGB,GDZS,SYL,MGYL, 
                    MGJZC,ZYLRL,ZYZZL,ZZCSYL,ZCFZBL,ZYYWSR,ZCZJ,FZZJ,GDQYHJ,GJJ,ZYYWLR,JLR,TZSY 
                    from [#TempTblFinanceHistory] 
                    where ([temp_id]>=@StartPageIndex)and ([temp_id] <@PageIndexEnd)                
             set nocount on
                DROP TABLE #TempTblFinanceHistory 
  end 
end 
GO 
 试试这个怎么样?

解决方案 »

  1.   

    在第一个declare上一行加上set nocount on
      

  2.   


    ---- 这里返回记录集了,应该是赋值给一个变量才对吧
    if @doCount != 0 
      begin 
      if @strWhere !='' 
      set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere 
      else 
      set @strSQL = 'select count(*) as Total from ' + @tblName 
      exec (@strSQL) 
      --print @strSQL 
      end 
      --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况-- 改成如下就不是返回记录集了set @strsql = 'select @totalrecords = count(*) from ' + @tblName + ' where '+@strWhere 
    exec(@strsql)
      

  3.   

    3楼,那里楼主是要专门统计记录数的,不能改,下面不就是整个else的吗
      

  4.   

    嗯,我先试试加上 set nocount on 看看结果