oracle中游标循环修改数据

    技术2022-07-14  88

    由于字段weight number(10,1)中存在数据,无法直接将精度改为number(10,2),所以需要按照如下步骤进行:

    第一步:新增临时字段:alter table class add weight_temp number(10,2)

    第二步:将值赋值给临时字段:update class set weight_temp=weight,weight=0

    第三步:删除原字段:alter table class drop column weight

    第四步:开始遍历赋值

    declare  cursor mycur is select prefix,no,sum(weight)whsweight from students group by prefix,no;  begin      for  n  in mycur loop          update class b set b.weight=n.whsweight where b.prefix=n.prefix and b.no=n.no;         exit when mycur%notfound;         end loop;         commit;    end;

    第五步:将临时字段(weight_temp)改为(weight)。

    Processed: 0.022, SQL: 9