用存储过程实现:一个公司为员工加薪,为工资在3000元以下的每次加100元,直到公司有一半的人工资在3000元以上,停止加薪,计算一共有多少人次加了薪?公司一共用去多少钱为员工加薪?    
CREATE PROCEDURE addgongzi AS
declare @lenth int
declare @num int
set @lenth=select count(*) from  gongz where price<3000
set @num=select count(*)/ 2 from  gongzi
while(@lenth<@num)
begin 
update gongzi   set price =price+100 where price<3000
set @lenth=select count(*) from  gongz where price<3000
set @num=select count(*)/2 from  gongzi
  @num+=@lenth
end 
这是错的    哪位高手指教一下

解决方案 »

  1.   

    try--
    CREATE PROCEDURE addgongzi AS 
    declare @lenth int 
    declare @num int 
    set @lenth=(select count(*) from  gongz where price <3000) 
    set @num=(select count(*)/ 2 from  gongzi)
    while(@lenth <@num) 
    begin 
      update gongzi  set price =price+100 where price <3000 
      set @lenth=(select count(*) from  gongz where price <3000) 
    end 
      

  2.   

    CREATE PROCEDURE addgongzi AS 
    declare @sum int=0 --员工人数 
    declare @renzi int=0 --总加薪人次
    declare @sumlower int --小于3000的人数
    select  @sum= count(*) from  gongziwhile(1=1) 
    begin 
     select @sumlower=count(*) from gongzi where price<3000;
     if( @sumlower<=@sum/2)break;
    select @renzi=@renzi+@sumlower;
    update gongzi  set price =price+100 where price <3000 
    end 
    -- @renzi为总加薪人次   加薪的金额是 @renzi*100