今天测试了一个结果如下:
create or replace type temp_type as varray(20) of varchar2(30) not null;--------定义数组;create table test
( col_1 number priamry key,
col_2 temp_type,
col_3 number
);--------------------------创建表insert into test --------------------------------测试数据
values(1,temp_type('我是中国人'),10000);insert into test
values(2,temp_type('你是美国人'),20000);insert into test
values(3,temp_type('你是日本人'),30000);insert into test
values(4,temp_type('你是阿富汗人'),40000);insert into test
values(5,temp_type('你是印度人'),20000);insert into test
values(6,temp_type('你是英国人'),20000);commit;----------------------检验输入SQL> select *
2 from test; COL_1 COL_2 COL_3
-------- ------------------------------ --------
1 TEMP_TYPE('我是中国人') 10000
2 TEMP_TYPE('你是美国人') 20000
3 TEMP_TYPE('你是日本人') 30000
4 TEMP_TYPE('你是阿富汗人') 40000
5 TEMP_TYPE('你是印度人') 20000
6 TEMP_TYPE('你是英国人') 20000已选择6行。有写了如下的程序块set serveroutput on
declaredata test%rowtype;cursor get_row(row_id in integer)
is
select *
from test
where col_1>row_id
for update of col_3; begin
for i in get_row(1)
loop
fetch get_row into data;
exit when get_row%notfound;
data.col_3:=50000;
update test
set col_3=data.col_3
where current of get_row;
end loop;commit;end;
/---------------运行结果是这样的
SQL> @h:\code\1.sqlPL/SQL 过程已成功完成。SQL> select *
2 from test; COL_1 COL_2 COL_3
-------- ------------------------------ --------
1 TEMP_TYPE('我是中国人') 10000
2 TEMP_TYPE('你是美国人') 20000
3 TEMP_TYPE('你是日本人') 50000
4 TEMP_TYPE('你是阿富汗人') 40000
5 TEMP_TYPE('你是印度人') 50000
6 TEMP_TYPE('你是英国人') 20000已选择6行。
-------------------------
为什么只有个别行进行了修改?????而不是2~6进行修改????
create or replace type temp_type as varray(20) of varchar2(30) not null;--------定义数组;create table test
( col_1 number priamry key,
col_2 temp_type,
col_3 number
);--------------------------创建表insert into test --------------------------------测试数据
values(1,temp_type('我是中国人'),10000);insert into test
values(2,temp_type('你是美国人'),20000);insert into test
values(3,temp_type('你是日本人'),30000);insert into test
values(4,temp_type('你是阿富汗人'),40000);insert into test
values(5,temp_type('你是印度人'),20000);insert into test
values(6,temp_type('你是英国人'),20000);commit;----------------------检验输入SQL> select *
2 from test; COL_1 COL_2 COL_3
-------- ------------------------------ --------
1 TEMP_TYPE('我是中国人') 10000
2 TEMP_TYPE('你是美国人') 20000
3 TEMP_TYPE('你是日本人') 30000
4 TEMP_TYPE('你是阿富汗人') 40000
5 TEMP_TYPE('你是印度人') 20000
6 TEMP_TYPE('你是英国人') 20000已选择6行。有写了如下的程序块set serveroutput on
declaredata test%rowtype;cursor get_row(row_id in integer)
is
select *
from test
where col_1>row_id
for update of col_3; begin
for i in get_row(1)
loop
fetch get_row into data;
exit when get_row%notfound;
data.col_3:=50000;
update test
set col_3=data.col_3
where current of get_row;
end loop;commit;end;
/---------------运行结果是这样的
SQL> @h:\code\1.sqlPL/SQL 过程已成功完成。SQL> select *
2 from test; COL_1 COL_2 COL_3
-------- ------------------------------ --------
1 TEMP_TYPE('我是中国人') 10000
2 TEMP_TYPE('你是美国人') 20000
3 TEMP_TYPE('你是日本人') 50000
4 TEMP_TYPE('你是阿富汗人') 40000
5 TEMP_TYPE('你是印度人') 50000
6 TEMP_TYPE('你是英国人') 20000已选择6行。
-------------------------
为什么只有个别行进行了修改?????而不是2~6进行修改????
begin
update test set col_3=50000;
end;
最后:update test set col_3=50000;
我拿你代码想按你思路给你写下,不过一精简,成这样啊,无语。
data testt%rowtype;
cursor get_row(row_id in integer)
is
select *
from testt
where col_1>row_id
for update of col_3; begin
for v_record in get_row(1) loop
data.col_3:=50000;
update testt
set col_3=data.col_3
where current of get_row;
end loop;
end;
这样写可以达到你目标
DECLARE
data test%ROWTYPE;
CURSOR get_row(row_id IN INTEGER) IS
SELECT * FROM test WHERE col_1 > row_id FOR UPDATE OF col_3;
BEGIN
OPEN get_row(1);
LOOP
FETCH get_row
INTO data;
EXIT WHEN get_row%NOTFOUND;
data.col_3 := 50000;
UPDATE test SET col_3 = data.col_3 WHERE CURRENT OF get_row;
END LOOP;
CLOSE get_row;
COMMIT;
END;
fetch get_row into data又fetch一行,即col=3,
所以每次修改的都是偶数fetch的那一行
----
将for i in get_row(1)改成open get_row(1)即可