PLSQL游标批量更新数据问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PLSQL游标批量更新数据问题相关的知识,希望对你有一定的参考价值。
declare
C_YIBAOHAO ABB.YIBAOHAO%TYPE;
C_XINGMING ABB.XINGMING%TYPE;
C_DIQUHAO ABB.DIQUHAO%TYPE;
cursor mycursor IS select YIBAOHAO,XINGMING,DIQUHAO from ABB where ID=(select ID from BBB);
BEGIN
OPEN mycursor;
LOOP
FETCH mycursor INTO C_YIBAOHAO,C_XINGMING,C_DIQUHAO;
EXIT WHEN mycursor%NOTFOUND;
IF BBB.YIBAOHAO=C_YIBAOHAO and BBB.XINGMING=C_XINGMING THEN
UPDATE BBB SET DIQUHAO=C_DIQUHAO;
ELSIF BBB.YIBAOHAO=C_YIBAOHAO and BBB.DIQUHAO=C_DIQUHAO THEN
UPDATE BBB SET XINGMING=C_XINGMING;
ELSIF BBB.XINGMING=C_XINGMING and BBB.DIQUHAO=C_DIQUHAO THEN
UPDATE BBB SET YIBAOHAO=C_YIBAOHAO;
END IF;
END LOOP;
close mycursor;
end;
/
目的是,2表ABB,BBB的YIBAOHAO,XINGMING,DIQUHAO字段各自对比,若任意2字段的值相同,剩余另一字段的表BBB的值更新为表ABB该字段的值
是oracle的,主要是2表 对比更新
3个 UPDATE比开游标划算:
举其中一种情况的例子:
UPDATE BBBSET BBB.DIQUHAO = (SELECT ABB.DIQUHAO FROM ABB WHERE ABB.ID = BBB.ID)
WHERE EXISTS(SELECT 1
FROM ABB
WHERE BBB.ID = ABB.ID
AND ABB.XINGMING = BBB.XINGMING
AND ABB.YIBAOHAO = BBB.YIBAOHAO); 参考技术A 两个问题:
1、在做update时,没有写条件,是要将BBB这个表的对应字段都更新成相同的么?
2、在判断语句中,直接引用BBB表字段的方法,可行么? 参考技术B PLSQL游标批量更新数据问题
UPDATE BBB
SET BBB.DIQUHAO = (SELECT ABB.DIQUHAO FROM ABB WHERE ABB.ID = BBB.ID)
WHERE EXISTS(SELECT 1
FROM ABB
WHERE BBB.ID = ABB.ID
AND ABB.XINGMING = BBB.XINGMING
AND ABB.YIBAOHAO = BBB.YIBAOHAO);
以上是关于PLSQL游标批量更新数据问题的主要内容,如果未能解决你的问题,请参考以下文章
oracle 中plsql 怎样写更新一行的语句(高手请进)