小计一次oracle存储过程的使用

Oracle 存储过程


功能要求,更新账号字段(账号有唯一约束)
如果“用户”表“姓名”字段不重复,则“帐号”字段改为“姓名”字段值,否则改为“英文名”字段值
注意:如果存在跨用户查询会报错,需要先授权

GRANT SELECT ON dc.ry TO wd;

create or replace procedure wd.updateZh
is 
    CURSOR collection1 IS select yhid,nvl(YH.xm,RY.xm) xm,ywm from DC.RY,WD.YH where ryid=yhid and zh is null;
    sameNum number;
    existNum number;
begin  
    FOR rec IN collection1 LOOP 
		select count(xm) into sameNum from wd.yh where xm = rec.xm;
		if sameNum > 1 then/*存在姓名相同是要用ywm去更新账号*/
			if rec.ywm is null then/*存在ywm是空的情况*/
				select count(zh) into existNum from wd.yh where zh like rec.xm||'%';
				if existNum > 0 then
					existNum:=existNum+1;/*必须要加一存在第一次插入zh是zhangsan1,第二个人员的xm是zhangsan的情况*/
					update wd.yh set zh = rec.xm||existNum where yhid=rec.yhid;
				else
					update wd.yh set zh = rec.xm where yhid=rec.yhid;
				end if;
			else/*ywm不为空的情况*/
				select count(zh) into existNum from wd.yh where zh like rec.ywm||'%';
				if existNum > 0 then
					existNum:=existNum+1;/*必须要加一存在第一次插入zh是zhangsan1,第二个人员的xm是zhangsan的情况*/
					update wd.yh set zh = rec.ywm||existNum where yhid=rec.yhid;
				else
					update wd.yh set zh = rec.ywm where yhid=rec.yhid;
				end if;
			end if;
		else/*没有同名的情况用xm去更新账号*/
			select count(zh) into existNum from wd.yh where zh = rec.xm;
			if existNum > 0 then
				update wd.yh set zh = rec.xm||existNum where yhid=rec.yhid;
			else 
				update wd.yh set zh = rec.xm where yhid = rec.yhid;
			end if;
		end if;
	end LOOP;  
	commit;
end updateZh; 
/


begin
  WD.updateZh();
end;
/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值