NB :我刚才重读了你的问题。 失败。 如果只有一个记录.我稍后会有新的更新.
现在开始吧.
的Oracledatabasepl/SQL用户指南和参考10g版本2(10.2 )型号b 14261-01参考
打开游标时,而不是取出游标时,所有行都将被重新锁定。 提交或回退事务时,这些行将被解锁。 由于行不再锁定,因此在提交后无法从FOR UPDATE游标读取。
所以不需要担心记录的解锁。
所以试试这个.
declare
cursor my table _ cur isselect * frommy _ tablewheresalary 50000 for update;
type my table _ ttistableofmytable _ cur % rowtype
INDEX BY PLS_INTEGER;
l_my_table_recs mytable_tt;
l_totalcount NUMBER;
比根
OPEN mytable_cur;
l_totalcount :=0;
LOOP
FETCH mytable_cur
bulkcollectintol _ my _ table _ RECs limit 100;
l _ total count 3360=l _ total count nvl (l _ my _ table _ RECs.count,0 );
--this is the check for only 1 row .
EXIT WHEN l_totalcount 2;
forindxin1. l _ my _ table _ RECs.count
LOOP
- processeachrecord . vial _ my _ table _ RECs (indx ) ) ) ) ) ) ) ) )
结尾环路;
EXIT WHEN mytable_cur%NOTFOUND;
结尾环路;
CLOSE mytable_cur;
结束;
ALTERNATE ANSWER我看着你的答案向后启动,想让你退出,如果有更多的话一排.不是正好有一个。 所以这是我以前的答案。
只有一个记录用两种简单的方法检查。
选项1 -显式抓住
declare
cursor C1 isselect * frommy _ tablewheresalary 50000 for update;
l_my_table_rec C1%rowtype;
l_my_table_rec2 C1%rowtype;
比根
开放C1;
fetch c1 into l_my_table_rec;
if c1%NOTFOUND then
--no数据基金
结束If;
fetch c1 into l_my_table_rec2;
if c1%FOUND THEN
--i have more then 1 row
结束If;
close c1;
- -处理逻辑
结束;
我希望你能考虑一下。
选择2 -异常捕获
declare
cursor C1 isselect * frommy _ tablewheresalary 50000 for update;
l_my_table_rec C1%rowtype;
比根
比根
选择*
from my_table
into l_my_table_rec
where salary 50000
for更新;
执行
when too_many_rows then
- handletheexceptionwheremorethanonerowisreturned
when no_data_found then
- handletheexceptionwherenorowsarereturned
when others then raise;
结束;
- -处理逻辑
结束;
另外,可以使用显式游标输入%变量来关闭游标记录而不是原始表。
这在添加到查询时特别有用。
此外,可以使用rememebr
更新表_ name
SET set_clause
WHERE CURRENT OF cursor_name;
类型语句更新表中的行,但如果还没有“获取”第二行,我想只有工作.
有关游标FOR循环的详细信息,请尝试Here