数据库里面有一个smst_test_access表,里面有列id,值类型是number ,现在我想更改这个列的值,我手工的把第一个列的值改成了数字1,然后想的是第二行的id是2,以后自动加一,不知道为什么,执行下面的存储过程就是不成功。
create or replace procedure cursor_test as t_id number;
t_idtemp number;
cursor new_cur is
select id from smst_test_access;begin
open new_cur;
fetch new_cur
into t_id;
while new_cur%found loop
if t_id = 1 then
update smst_test_access set smst_test_access.id = 1;
t_idtemp:=t_idtemp+1;
else
update smst_test_access set smst_test_access.id = t_idtemp;
t_idtemp:=t_idtemp+1;
end if;
end loop;
commit;
close new_cur;
end;
create or replace procedure cursor_test as t_id number;
t_idtemp number;
cursor new_cur is
select id from smst_test_access;begin
open new_cur;
fetch new_cur
into t_id;
while new_cur%found loop
if t_id = 1 then
update smst_test_access set smst_test_access.id = 1;
t_idtemp:=t_idtemp+1;
else
update smst_test_access set smst_test_access.id = t_idtemp;
t_idtemp:=t_idtemp+1;
end if;
end loop;
commit;
close new_cur;
end;
解决方案 »
- 应用程序SQL跟踪sql_trace使用方法?
- orcale的数据库响应时间
- oracle sql块的执行
- 求个sql语句!
- 存储过程中传变量到like 后面,就是得不到想要的结果,把变量改为常量就行了,帮我看看
- 公司的领导想让我学习做报表,我初接触ORACLE,有什么合适的书能让我快速上手的?
- 删除记录
- create table aaa(id varchar2(20)) 与create table aaa(id varchar2(20) where 1有什么不同
- oracle是否支持top关键字,比如select top 1 name from table_friend
- 请教高手,导入出现错误
- C#.net连接oracle8i出现乱码
- 触发器问题。没有数据?
这个一UPDATE你的表中的ID全部变成1了 循环也没有什么用每次更新都更新成t_idtemp这个值 加个WHERE条件限制或者直接用一个UPDATE语句就搞顶了update update smst_test_access set smst_test_access.id =rownum;
这样也不用写循环 也不用PL/SQL了 直接一个SQL就搞定的
SELECT id FROM smst_test_access FOR UPDATE OF id;
r1 c1%ROWTYPE;
t_id NUMBER(10, 0);BEGIN
t_id := 0;
FOR r1 IN c1 LOOP
UPDATE smst_test_access SET id = t_id WHERE CURRENT OF c1;
t_id:=t_id+1;
END LOOP;
END;