当前位置: 代码迷 >> Oracle管理 >> 循环语句解决思路
  详细解决方案

循环语句解决思路

热度:20   发布时间:2016-04-24 05:31:02.0
循环语句
2.给学生表增加一百个学生分别为.net1...net100,序号是奇数时学生的性别为男,否则性别为女,能被7整除或以7为尾数时的学生不插入表中.


declare 
  sex char(2);
  sname varchar2(20);
begin
  for i in 1..100 loop
  sname:='.net'||i;
  if mod(i,2)<>0 then
  sex:='男';
  else
  sex:='女';
  end if;
  if mod(i,7)=0 or mod(i,10)=7 then
  goto a;
  end if;
  insert into student2 values(i,sname,sex);
  <<a>>
  end loop;
end;



大家帮忙看以下哪里有错呀?

------解决方案--------------------
if mod(i,7)!=0 and mod(i,10)!=7 then
insert into a values(i,sname);
end if;

改成这样
------解决方案--------------------
declare
sex char(2);
sname varchar2(20);
begin
for i in 1..100 loop
sname:='.net'||i;
if mod(i,2)<>0 then
sex:='男';
else
sex:='女';
end if;
if mod(i,7)!=0 and mod(i,10)!=7 then
insert into bgsyao04 values(i,sname);
end if;
end loop;
end;
------解决方案--------------------

SQL code
declare    sex char(2);  sname varchar2(20);begin  for i in 1..100 loop  sname:='.net'||i;  if mod(i,2)<>0 then  sex:='男';  else  sex:='女';  end if;  if mod(i,7)=0 or mod(i,10)=7 then  goto a;  end if;  insert into student2 values(i,sname,sex);  <<a>>  loop  exit;  end loop;  end loop;end;
------解决方案--------------------
建议你学习下 标签
SQL code
declare    sum_n number(5):=0;begin  for i in 1..10   loop  if mod(i,2)=0then   goto a;elsesum_n:=sum_n+i;  end if;  dbms_output.put_line(sum_n);  <<a>> begin   continue; end a;   end loop;end;
  相关解决方案