Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 使用pl sql过程在内部循环中更新语句_Oracle - Fatal编程技术网

Oracle 使用pl sql过程在内部循环中更新语句

Oracle 使用pl sql过程在内部循环中更新语句,oracle,Oracle,我试图用下面的pl sql过程将我的表更新到field1,编译和执行没有任何错误,当我调用这个过程时,更新不起作用,我不知道为什么!我用来更新。。。。带有光标的当前语句和代码如下 create or replace procedure p1 is r1 table1%rowtype; r2 table2%rowtype; cursor c1 is select * from table1 for update of field1; cursor c2 is select * from table

我试图用下面的pl sql过程将我的表更新到field1,编译和执行没有任何错误,当我调用这个过程时,更新不起作用,我不知道为什么!我用来更新。。。。带有光标的当前语句和代码如下

create or replace procedure p1 is
r1 table1%rowtype;
r2 table2%rowtype;
cursor c1 is select * from table1 for update of field1;
cursor c2 is select * from table2;
begin
open c1;
loop <<outer>>
    fetch c1 into r1;
    open c2;
        loop <<inner>>
            fetch c2 into r2;
               if condition then
               dbms_output.put_line('ok');            
               update table1
               set field1= 1
               where current of c1;                    
               end if;
        exit when c2%notfound;
        end loop inner;       
    close c2; 
    exit when c1%notfound;
end loop outer;
close c1;
end;
/

注意:IF语句中的条件是正确的,因为当我执行该过程时,语句dbms_output.put_line'ok';当我删除update语句和for update of…current of语句时,每次执行循环时都会成功执行,但当我将update语句与for update of…current of语句放在相同条件下时,update语句不起作用。

如何检查更新是否起作用


如果在另一个会话中查询该表,则在pl/sql代码之后执行commit语句之前,不会看到代码所做的任何更改

感谢重播,这个过程将花费很多时间,所以我尝试运行这个过程大约15分钟,然后我取消了执行,但是每次循环执行时,至少程序应该在dbms窗口中打印OK,但是当我取消运行时,没有输出!!!!!我把commit语句放在update中的where条件和end之间,如果像update cdr_table set ex_flag=1,cdr_cur的当前值;犯罪如果结束;而且它不起作用@user2470764看起来像是您在另一个打开的会话中阻止了c1游标检索到的行。您根本不需要循环。据我所知,从你模糊的例子,这可能都可以通过一个更新语句来完成。+1到@a_horse_和_no_名称。我还想补充一点,为c2的每一行更新c1的每一行是没有意义的。您将一次又一次地更新同一行。这里不需要plsql。
1)

    open c2;
        loop <<inner>>
            fetch c2 into r2;
               if condition then
               dbms_output.put_line('ok');            
               update table1
               set field1= 1
               where current of c1;  -- <---- YOU CANNOT USE THAT
               end if;
        exit when c2%notfound;
        end loop inner;       
    close c2; 

2) Rewrite to plain SQL:

CREATE TABLE table1(field1 NUMBER, field2 NUMBER);
CREATE TABLE table2(field1 NUMBER, field2 NUMBER);

INSERT INTO table1(field1, field2) VALUES(111, 121);
INSERT INTO table1(field1, field2) VALUES(112, 122);
INSERT INTO table1(field1, field2) VALUES(113, 123);
INSERT INTO table1(field1, field2) VALUES(114, 124);
INSERT INTO table1(field1, field2) VALUES(115, 125);

INSERT INTO table2(field1, field2) VALUES(111, 121);
INSERT INTO table2(field1, field2) VALUES(112, 122);
INSERT INTO table2(field1, field2) VALUES(213, 123);
INSERT INTO table2(field1, field2) VALUES(214, 124);
INSERT INTO table2(field1, field2) VALUES(215, 125);
COMMIT;

UPDATE  table1
SET     field1 = 1
WHERE   EXISTS
(
        SELECT  1
        FROM    table2
        WHERE   table1.field1 = table2.field1
        AND     table1.field2 = table2.field2
);
-- 2 rows updated.

SELECT * FROM table1;
/*
1   121
1   122
113 123
114 124
115 125
*/