您的位置:首页 > 其它

orcl中的赋值语句,case,if elsif,while 循环,for

2012-09-21 19:56 483 查看
一. Orcl中的赋值

1. :=
方法赋值


定义两个变量 mynum
类型为varchar2和mid类型为number

declare mynum varchar2(50):='abc'; mid number:=1;

2. 使用select into方式赋值

declare nums product.typeid%type;

begin 将商品表ID为1的typeid赋给nums

select typeid into nums from product where productid=1;

dbms_output.put_line('类型ID:' || nums);
打印

end;

二.Case的使用

--1、case 表达式 when 值1 then when 值2 then else 其他值 end case

--2、case when 逻辑判断 then when 逻辑判断2 then end case;

例一:

declare n1 number;n2 varchar2(20);

begin

n1:=&abc; 淡出输入框abc为参数名 为n1赋值

case n1

when 1 then n2:='值为1';

when 2 then n2:='值为2';

else n2:='其他值';

end case;

dbms_output.put_line(n2);

end;

三.循环的使用

1.使用loop无条件循环
计算1-100之和

declare i number:=1;total number:=0;
声明变量 i 和 number

begin

loop
开始循环

total:=total+i;

i:=i+1;

if i>100 then

exit; 退出循环

end if;
结束if

end loop;
结束循环

dbms_output.put_line('最终结果:' || total);

end;

2.使用while..loop
实现有条件循环


declare i number:=1;total number:=0;

begin

while i<=100 loop
判断条件

total:=total+i;

i:=i+1;

end loop;
结束循环

dbms_output.put_line('最终结果:' || total);

end;

3、使用for...loop
实现固定次数的循环


declare i number:=1;total number:=0;

begin

for i in 1..100 loop
循环的条件

total:=total+i;

end loop;

dbms_output.put_line('最终结果:' || total);

end;

四.异常处理

根据输入的id号查询产品表的某产品名

declare ids number;pname varchar2(50);

begin

ids:=&abc; 输入需要查询的id

select productname into pname from product where productid=ids;

dbms_output.put_line('找到数据:' || pname);

exception

when no_data_found then

dbms_output.put_line('异常:没有找到数据');

when others then

dbms_output.put_line('其他异常:' || sqlerrm);

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐