游标中做更新操作的两种用法
2006-07-03 11:34
399 查看
--游标中做更新操作的两种用法
--表结构
Create Table agentmgr.yangbc_test1(Id Number(6), Name Varchar2(64));
Select t.*,Rowid From agentmgr.yangbc_test1 t;
--1:
/*在你取完部分数据并执行的过程中,可能有commit或者rollback语句,导致在表t上加的lock被释放掉,
再取数据的时候导致出错。*/
declare
cursor hh is select Name from yangbc_test1 for update;
v_Name Varchar2(64):='';
begin
open hh;
loop
fetch hh into v_Name;
exit when hh%notfound;
update yangbc_test1 set Id='18' where current of hh;
--commit; -- releases locks,去掉就可以了
dbms_output.put_line('v_Name='||v_Name);
end loop;
Commit;--在这里提交
close hh;
end;
--2:
/*If you want to fetch across commits, do not use the FOR UPDATE and CURRENT OF
clauses. Instead, use the ROWID pseudocolumn to mimic the CURRENT OF clause.
Simply select the rowid of each row into a UROWID variable. Then, use the rowid to
identify the current row during subsequent updates and deletes. An example
follows:*/
declare
cursor hh is select Name,Rowid from yangbc_test1;
v_Name Varchar2(64):='';
v_rowid UROWID;
begin
open hh;
loop
fetch hh into v_Name,v_rowid;
exit when hh%notfound;
update yangbc_test1 set Id='17' Where Rowid=v_rowid;
commit; -- releases locks
dbms_output.put_line('v_Name='||v_Name);
end loop;
close hh;
end;
--表结构
Create Table agentmgr.yangbc_test1(Id Number(6), Name Varchar2(64));
Select t.*,Rowid From agentmgr.yangbc_test1 t;
--1:
/*在你取完部分数据并执行的过程中,可能有commit或者rollback语句,导致在表t上加的lock被释放掉,
再取数据的时候导致出错。*/
declare
cursor hh is select Name from yangbc_test1 for update;
v_Name Varchar2(64):='';
begin
open hh;
loop
fetch hh into v_Name;
exit when hh%notfound;
update yangbc_test1 set Id='18' where current of hh;
--commit; -- releases locks,去掉就可以了
dbms_output.put_line('v_Name='||v_Name);
end loop;
Commit;--在这里提交
close hh;
end;
--2:
/*If you want to fetch across commits, do not use the FOR UPDATE and CURRENT OF
clauses. Instead, use the ROWID pseudocolumn to mimic the CURRENT OF clause.
Simply select the rowid of each row into a UROWID variable. Then, use the rowid to
identify the current row during subsequent updates and deletes. An example
follows:*/
declare
cursor hh is select Name,Rowid from yangbc_test1;
v_Name Varchar2(64):='';
v_rowid UROWID;
begin
open hh;
loop
fetch hh into v_Name,v_rowid;
exit when hh%notfound;
update yangbc_test1 set Id='17' Where Rowid=v_rowid;
commit; -- releases locks
dbms_output.put_line('v_Name='||v_Name);
end loop;
close hh;
end;
相关文章推荐
- 游标中做更新操作的两种用法
- 方法更新Linq中两种更新操作
- github 两种常要操作的用法-fork+pull
- Android线程的三种用法和子线程更新UI两种用法
- 游标操作,使用游标,更新数据
- Linq中两种更新操作
- Sublime Text 3 插件用法以及详细快捷键操作 【2014-4-18再详细更新】
- Linq中两种更新操作
- mysql游标示例 mysql游标简易教程 mysql批量操作数据 mysql批量更新数据 mysql逐条更新数据
- 在游标中嵌套查询或更新操作示例
- SQL Server里函数的两种用法(可以代替游标)
- 更新游标的用法
- 【C】二级指针探秘 & 星号的两种用法(1.与基本类型结合形成另一种类型,比如与int结合形成int* 2.取值操作)
- ABAP report常用的一些语句用法(包括字符串操作,变量动态操作)(持续更新)
- 我所接触到的一些常用Linux命令和用法(初学者献丑了)每日更新.......
- 软键盘输入法操作相关(持续更新中)
- MySQL 客户端游标未启用导致BLOB更新失败
- 关于父子结构表的两种操作
- 如:有个某市所有移动用户号码记录资料表需要更新,你将如何操作?
- 更新或删除某条记录的时候,无法操作,用锁解决