1、2、3种存储过程操作例子

结构:declare

定义变量:三种定义方式

 1、非面向对象: num1  number:=0;  num2 number;

 2、半面向对象:num1  表名.列名%type;

 3、对象:num1  表%Rowtype;

  begin 里面写方法、判断、输出等操作。。。。。。

   end;  结束

例子

-----------if then else end if结构
declare
  tname scott.emp.ename%type;
begin
  select ename into tname from scott.emp where ename='SMITH';
  if tname='SMITH'
     then update scott.emp set ename='xxx' where ename='SMITH';
  end if;
  commit;
  DBMS_OUTPUT.PUT_LINE('修改成功');     
end
;

--------case when then end case结构
declare
   salay scott.emp.sal%type;
begin
   select sal into salay from scott.emp where ename='SMITH';
   case
     when salay>10000
       then update scott.emp set sal=sal-5000;
     when salay between 8000 and 10000
       then update scott.emp set sal=sal-2000;
     else
       update scott.emp set sal=sal+1000;
     end case; 
commit;
end
;

 

------------循环结构
---1到一百相加
declare
   i number:=0;
   sum1 number:=0;
begin
   while(i<100)
   loop
   i:=i+1;
   sum1:=sum1+i; 
   end loop;
   DBMS_OUTPUT.PUT_LINE(sum1);
end;
--------1到100偶数和
declare
   i number:=0;
   sum1 number:=0;
begin
   while(i<100)
   loop
     i:=i+1;
     if(mod(i,2)=0) 
     then sum1:=sum1+i;
     end if;
   end loop;
   DBMS_OUTPUT.PUT_LINE(sum1);
end;

--员工表工资大于8000的占三分之二以上  
declare
  sumperson number:=0;
  baqianperson number:=0;
begin
  select count('X') into sumperson from scott.emp;
  select count('X') into baqianperson from scott.emp where sal>8000;
  while(baqianperson/sumperson<0.66)
  loop
    update scott.emp set sal=sal+100;
    select count('X') into baqianperson from scott.emp where sal>8000;
  end loop;
  commit;
  DBMS_OUTPUT.PUT_LINE('操作成功');
end
;

转载于:https://www.cnblogs.com/xiaohujay/p/8709756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值