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

一.    Orcl中的赋值

1.          := 方法赋值

定义两个变量 mynum 类型为varchar2mid类型为number

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

2.          使用select into方式赋值

   declare nums product.typeid%type;

begin   将商品表ID1typeid赋给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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值