if exists(select * from sysobjects where type='p' and name='sp_tjbb5_new')
   drop procedure sp_tjbb5_new
go
CREATE procedure sp_tjbb5_new @paraA varchar(20),@paraB varchar(20),@paraC varchar(20),@paraD varchar(20)
as
set nocount onif exists (select * from sysobjects where id = object_id(N'[tjbb5]') 
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
truncate table [tjbb5]
end
if exists (select * from sysobjects where id = object_id(N'[tjbb51]') 
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
truncate table [tjbb51]
end
declare @TimeName            nvarchar(25),
        @wxdw                varchar(40),
        @str                 varchar(1000)
declare @TableStr            nvarchar(1000), 
        @SelectStr           nvarchar(1000),
        @SelectStr1          nvarchar(1000),
        @ColumnStr           nvarchar (1000)select @SelectStr='Select distinct 维修单位 from tjbb_view5 where 维修单位<>'''' '
                  + ' order by 维修单位'select @str='insert into tjbb5' +'(维修单位)'+' ' + @SelectStr
exec(@str)                        
select @str='Declare tjbb5_Cursor Cursor for ' + @SelectStrexec(@str) 
 open tjbb5_Cursor
 fetch tjbb5_Cursor into @wxdw
 while(@@fetch_status=0) 
    begin
      select @SelectStr='Select sum(item) as servicecount from (select count(*) as item from (select count(*) as
             total,车牌号码,车辆类别,检测编号 from tjbb_view5 where 维修单位='''+@wxdw+''' and 记录状态<>''3'''
      select @SelectStr=@SelectStr+' group by 车牌号码,车辆类别,检测编号) a ) as b'
      select @str='update tjbb5'+' set 维修辆次=( ' + @SelectStr + ')' 
                   + ' where 维修单位='''+@wxdw+''''
      exec(@Str) 
      select @SelectStr1='Select distinct 运管所 from tjbb_view5 where 维修单位='''+@wxdw+''' '  
      select @Str='update tjbb5'+' set 运管所=('+ @SelectStr1 +')'
                   + ' where 维修单位='''+@wxdw+''''
      exec(@Str)     
      select @SelectStr1='Select min(检测日期) as item from tjbb_view5 where 维修单位='''+@wxdw+''' '  
      select @Str='update tjbb5'+' set 检测日期=('+ @SelectStr1 +')'
                   + ' where 维修单位='''+@wxdw+''''
      exec(@Str) 
      select @SelectStr1='Select sum(item) as servicecount from (select count(*) as item from (select count(*) as
                   total,车牌号码,车辆类别,检测编号 from tjbb_view5 where 维修单位='''+@wxdw+''' and 记录状态<>''3''
                   and 检测次数=1 and (检测结果<>''X'' or 检测结果<>''x'')'
      select @SelectStr1=@SelectStr1+' group by 车牌号码,车辆类别,检测编号) a ) as b'
      select @str='update tjbb5'+  ' set 一次合格率=100.00*(' + @SelectStr1 +')'+ '/(' 
                  + @SelectStr+ ')'  + ' where 维修单位='''+@wxdw+''''
      exec(@str)         
      select @SelectStr1='Select sum(item) as servicecount from (select count(*) as item from (select count(*) as
                   total,车牌号码,车辆类别,检测编号 from tjbb_view5 where 维修单位='''+@wxdw+''' and 记录状态<>''3''
                   and 检测次数<=2 and (检测结果<>''X'' or 检测结果<>''x'')'
      select @SelectStr1=@SelectStr1+' group by 车牌号码,车辆类别,检测编号) a ) as b'
      select @str='update tjbb5'+  ' set 二次合格率=100.00*(' + @SelectStr1 +')'+ '/(' + 
                  @SelectStr+ ')' + ' where 维修单位='''+@wxdw+''''
      exec(@str)       
      select @SelectStr1='Select sum(检测次数) as item from tjbb_view5 where 
                  维修单位='''+@wxdw+'''  '
      select @str='update tjbb5'+ ' set 平均检测次数=1.0*(' + @SelectStr1 +')'+'/('+@SelectStr+')'
                  + ' where 维修单位='''+@wxdw+''''
      exec(@str)          
      select @SelectStr1='Select sum(复检项数) as item from tjbb_view5 where 
                 维修单位='''+@wxdw+''' '
   
      select @str='update tjbb5'+ ' set 平均复检项数=1.0*(' + @SelectStr1 +')'+'/('+@SelectStr+')'
                  + ' where 维修单位='''+@wxdw+''''
      exec(@str)        
      select @SelectStr='Select ('''+@paraA+'''*二次合格率+'''+@paraB+'''*平均检测次数+'''+@paraC+'''*平均复检项数+
                        '''+@paraD+''') as item from tjbb5'+' where 维修单位='''+@wxdw+''''
     
      select @str='update tjbb5'+  ' set 综合得分=('+ @SelectStr + ')'    
                       + ' where 维修单位='''+@wxdw+''''
      exec(@str)
      fetch tjbb5_Cursor into @wxdw
    end 
close tjbb5_Cursor
deallocate tjbb5_Cursorselect @SelectStr='select 维修单位,维修辆次,运管所,检测日期,一次合格率,二次合格率,平均检测次数,
                   平均复检项数,综合得分 from tjbb5' +  ' order by 综合得分 desc'
select @SelectStr1='insert into tjbb51' + '(维修单位,维修辆次,运管所,检测日期,一次合格率,
                    二次合格率,平均检测次数,平均复检项数,综合得分)' +@SelectStr
exec(@SelectStr1)exec('select * from tjbb51' + ' order by wb')
set nocount off
GOexec sp_tjbb5_new '1','2','3','4'
go

解决方案 »

  1.   

    出错提示:
    Warning: Null value eliminated from aggregate.
    Warning: Null value eliminated from aggregate.
    Server: Msg 512, Level 16, State 1, Line 1
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
    The statement has been terminated.
    Warning: Null value eliminated from aggregate.
    Warning: Null value eliminated from aggregate.
      

  2.   

    1 绍兴市新三角轿车维修有限公司 2 绍兴市区 2002-11-12 00:00:00.000 .00 100.00 4.50 2.00 119.00
    2 上虞市建新汽车修理厂 1 上虞市 2002-11-29 00:00:00.000 .00 100.00 3.00 2.00 116.00
    3 绍兴市越州轿车修理厂 1 绍兴市区 2002-11-12 00:00:00.000 .00 100.00 3.00 2.00 116.00
    4 虞伟汽修厂 1 上虞市 2002-11-28 00:00:00.000 100.00 100.00 2.00 2.00 114.00
    5 浙江中成建工集团有限公司汽车修理分公司 4 绍兴市区 2002-11-07 00:00:00.000 25.00 100.00 2.25 .75 110.75
    6 绍兴市申浙汽车有限公司 3 绍兴市区 2002-11-07 00:00:00.000 100.00 100.00 1.00 .00 106.00
    7 绍兴县第三汽车修理厂 4 绍兴市区 2002-11-05 00:00:00.000 100.00 100.00 1.00 .00 106.00
    8 绍兴市万众汽车修理有限公司 3 绍兴市区 2002-11-06 00:00:00.000 33.33 66.67 4.67 4.00 92.01
    9 绍兴市交通轿车维修有限责任公司保修厂 3 绍兴市区 2002-11-08 00:00:00.000 .00 66.67 4.00 3.00 87.67
    10 D02 10 新昌县 2002-11-25 00:00:00.000 .00 60.00 6.70 3.30 87.30
    11 无 4 新昌县 2002-11-25 00:00:00.000 .00 50.00 12.00 2.75 86.25
    12 上虞市虞中汽车修配厂 2 上虞市 2002-11-29 00:00:00.000 .00 50.00 9.00 4.50 85.50
    13 绍兴市越城汽车修理厂 5 绍兴市区 2002-11-06 00:00:00.000 .00 60.00 4.60 1.80 78.60
    14 东达汽修厂 2 上虞市 2002-11-28 00:00:00.000 .00 50.00 5.50 4.50 78.50
    15 汽运汽修厂 2 上虞市 2002-11-28 00:00:00.000 .00 50.00 4.50 5.00 78.00
    16 绍兴市越风汽车修理有限责任公司 6 绍兴市区 2002-11-06 00:00:00.000 33.33 50.00 5.83 3.00 74.66
    17 中国轻纺城汽车服务中心 3 绍兴市区 2002-11-06 00:00:00.000 66.67 66.67 1.00 .33 73.66
    18 绍兴县福全汽车修理厂 1 绍兴市区 2002-11-06 00:00:00.000 .00 .00 21.00 8.00 70.00
    19 浙江省对外贸易运输绍兴分公司修理厂 11 绍兴市区 2002-11-05 00:00:00.000 18.18 45.45 4.18 4.00 69.81
    20 绍兴市北海汽车修理有限责任公司 4 绍兴市区 2002-11-05 00:00:00.000 .00 50.00 3.25 3.00 69.50
    21 上海大众汽车绍兴第一特约维修站 2 绍兴市区 2002-11-06 00:00:00.000 .00 50.00 3.00 2.00 66.00
    22 太维汽修厂 1 上虞市 2002-11-28 00:00:00.000 .00 .00 19.00 8.00 66.00
    23 D01 7 新昌县 2002-11-25 00:00:00.000 .00 42.86 3.43 4.00 65.72
    24 绍兴市交通运输公司汽车修理厂 11 绍兴市区 2002-11-05 00:00:00.000 27.27 45.45 3.27 3.00 64.99
    25 跃进汽修厂 2 上虞市 2002-11-28 00:00:00.000 .00 50.00 3.00 1.50 64.50
    26 绍兴市交通轿车维修有限责任公司 19 绍兴市区 2002-11-06 00:00:00.000 26.32 47.37 2.74 2.26 63.63
    27 通联汽修厂 2 上虞市 2002-11-28 00:00:00.000 50.00 50.00 1.50 2.00 63.00
    28 上虞市炼塘汽车修理厂 2 NULL 2002-11-13 00:00:00.000 .00 50.00 2.00 1.00 61.00
    29 绍兴市华丰汽车修理厂 9 绍兴市区 2002-11-06 00:00:00.000 33.33 44.44 2.78 2.11 60.33
    30 绍兴县平江汽车修理厂 6 绍兴市区 2002-11-08 00:00:00.000 16.67 33.33 5.50 3.83 59.82
    31 D05 10 新昌县 2002-11-25 00:00:00.000 .00 30.00 5.40 5.00 59.80
    32 绍兴县运输保养厂 5 绍兴市区 2002-11-07 00:00:00.000 40.00 40.00 4.60 2.20 59.80
    33 正洲汽修厂 3 上虞市 2002-11-28 00:00:00.000 33.33 33.33 6.67 3.00 59.67
    34 建新汽修厂 1 上虞市 2002-11-29 00:00:00.000 .00 .00 15.00 8.00 58.00
    35 百运汽修厂 3 上虞市 2002-11-29 00:00:00.000 .00 .00 12.00 9.33 55.99
    36 D04 6 新昌县 2002-11-27 00:00:00.000 .00 16.67 6.83 7.00 55.33
    37 绍兴市康利汽车修理厂 8 绍兴市区 2002-11-06 00:00:00.000 37.50 37.50 3.25 2.25 54.75
    38 市运汽修厂 5 上虞市 2002-11-28 00:00:00.000 .00 20.00 7.40 5.20 54.40
    39 炼塘汽修厂 3 上虞市 2002-11-28 00:00:00.000 .00 33.33 4.00 2.67 53.34
    40 D06 12 新昌县 2002-11-26 00:00:00.000 .00 8.33 8.50 7.33 51.32
    41 绍兴市金中汽车修理厂 15 绍兴市区 2002-11-06 00:00:00.000 20.00 33.33 2.53 2.60 50.19
    42 绍兴市万通轿车服务有限公司 7 绍兴市区 2002-11-06 00:00:00.000 14.29 28.57 4.14 2.86 49.43
    43 D17 42 新昌县 2002-11-25 00:00:00.000 .00 21.43 4.71 4.60 48.65
    44 绍兴市越风汽车修理有限责任公司客修厂 17 绍兴市区 2002-11-06 00:00:00.000 23.53 29.41 3.71 2.59 48.60
    .......
    ........
    .......
    81 绍兴县华舍汽车修理厂 1 绍兴市区 2002-11-12 00:00:00.000 .00 .00 1.00 1.00 9.00
    82 绍兴市国脉通信有限责任公司汽车摩托车维修分公司 NULL NULL NULL NULL NULL NULL NULL NULL
      

  3.   

    1
    sum()项中有可能有null
    比如sum(a)改为sum(isnull(a,0))
      

  4.   


          select @SelectStr='Select sum(item) as servicecount from (select count(*) as item from (select count(*) as
                 total,车牌号码,车辆类别,检测编号 from tjbb_view5 where 维修单位='''+@wxdw+''' and 记录状态<>''3'''
          select @SelectStr=@SelectStr+' group by 车牌号码,车辆类别,检测编号) a ) as b'
          select @str='update tjbb5'+' set 维修辆次=( ' + @SelectStr + ')' 
                       + ' where 维修单位='''+@wxdw+''''
          exec(@Str) 
    --------------set 后边的select 语句得到的可能是一个数据集而不是一个值。,检查类似语句。
      

  5.   

    可是前面都算出来了,单单是最后一条不行,而这一条是有数据的,sum(a)改为sum(isnull(a,0))还是一样
      

  6.   

    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.很明白啊,你的自查询查出来的是一个集合但是你用了=
      

  7.   

    警告信息是说sum(a)要改为sum(isnull(a,0))
    错误信息是说set后面的可能是一个数据集而不是一个值
    我看十有八九是:
    ---------------------------------
    select @SelectStr1='Select distinct 运管所 from tjbb_view5 where 维修单位='''+@wxdw+''' '  
          select @Str='update tjbb5'+' set 运管所=('+ @SelectStr1 +')'
                       + ' where 维修单位='''+@wxdw+''''
          exec(@Str)     
    --------------------------------
    select @SelectStr='Select ('''+@paraA+'''*二次合格率+'''+@paraB+'''*平均检测次数+'''+@paraC+'''*平均复检项数+
                            '''+@paraD+''') as item from tjbb5'+' where 维修单位='''+@wxdw+''''
         
          select @str='update tjbb5'+  ' set 综合得分=('+ @SelectStr + ')'    
                           + ' where 维修单位='''+@wxdw+''''
          exec(@str)
    -----------------------
    两段有错!你先注释掉看看
      

  8.   

    谢谢 sky_blue(老衲),pengdali(大力)两位。
    我昨天晚上找了老半天,发现了是有一条记录有重复。