for loop 循环判断问题

wangxuesong5 2009-04-22 05:16:14
begin
for a in(select avalue from test2)
loop
if(a.avalue=:new.avalue)
then
exit ;
else
insert into test2(name,avalue) values('awea',:new.avalue);
end if ;
end loop;
end;
关键问题是 if只判断了一次 比如判断两表第一行数据后 如果不相等 就执行else语句
很无奈
...全文
133 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
得8分的原因 <> 以及创新和参与
谢谢
4分 说得也对 但不是key
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
declare b number;
begin

b:=0;
for a in(select avalue from test2)
loop
if(a.avalue<>:new.avalue)
then
NULL ;
else
b:=1;
end if ;
end loop;
if b=0
then
insert into test2(name,avalue) values('awea',:new.avalue);
end if;
end;

正解 大家都有分 呵呵
阿三 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wangxuesong5 的回复:]
我就是比较较真 为什么if语句只能判断表里的第一条记录 就执行else了
[/Quote]

你是指的你最開始寫的嗎?
因為你在if(a.avalue=:new.avalue)
then
exit ;

exit就跳出循環了,不再執行其它操作了
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
我就是比较较真 为什么if语句只能判断表里的第一条记录 就执行else了
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
oracle9i
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
恩恩 向test1 插入的时候 触发 判断test2是否有相同数据 没有的话给test2也插入 有的话就不用插入了
阿三 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wangxuesong5 的回复:]
555555 插入的时候插入相同的两条数据
[/Quote]

你是在觸發器中寫的如上代碼吧?
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
555555 插入的时候插入相同的两条数据
wangxuesong5 2009-04-22
  • 打赏
  • 举报
回复
555555 插入的时候插入相同的两条数据
阿三 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bw555 的回复:]
用游标实现吧
SQL code
declare
cursor s is select avalue from test2;
number a;
begin
open s;
fetch s into a;
while a<>:new.avalue and a %found loop
insert into test2(name,avalue) values('awea',:new.avalue);
fetch s into a;
end loop;
close s;
END;


[/Quote]
樓主的方法也是用的游標,隱式游標~不知記錯沒有:(
bw555 2009-04-22
  • 打赏
  • 举报
回复
修正下
declare
cursor s is select avalue from test2;
number a;
begin
open s;
fetch s into a;
while a<>:new.avalue and s %found loop
insert into test2(name,avalue) values('awea',:new.avalue);
fetch s into a;
end loop;
close s;
END;
bw555 2009-04-22
  • 打赏
  • 举报
回复
用游标实现吧
declare
cursor s is select avalue from test2;
number a;
begin
open s;
fetch s into a;
while a<>:new.avalue and a %found loop
insert into test2(name,avalue) values('awea',:new.avalue);
fetch s into a;
end loop;
close s;
END;
阿三 2009-04-22
  • 打赏
  • 举报
回复
if(a.avalue<>:new.avalue) then
insert into test2(name,avalue) values('awea',:new.avalue);
end if ;

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧