Oracle笔记之存储过程经典案例

概述
本篇博文中主要探讨以下内容:

  1. 记录变量(record复合数据类型)
  2. sql 属性(SQL%FOUND、SQL%NOTFOUND、SQL%ROWCOUNT)
  3. 输入一个部门号,按不同的部门进行加薪
  4. 一次取多个值用select into
  5. 取出全部列

1. 记录变量

record:
定义记录数据类型,将几个相关的、分离的、基本数据类型的变量组成一个整体的方法,即RECORD复合数据类型。

在使用记录类型变量时,需要在声明部分先声明记录的组成,记录的变量,然后在执行部分引用该记录变量本身或者其中的成员。

--记录变量
declare 
       type emp_record_type is record(v_name emp.ename%type,v_sal emp.sal%type);
       emp_record emp_record_type;
begin
       select ename ,sal into emp_record from emp where empno=&no;
       dbms_output.put_line('name: ' || emp_record.v_name || '  sal: ' || emp_record.v_sal);
end;


declare 
  emp_record emp%ROWTYPE;
begin
  select * into emp_record from emp where empno = &no;
  dbms_output.put_line('name:  ' || emp_record.ename || ' sal: ' || emp_record.sal);
exception 
  when NO_DATA_FOUND then
     dbms_output.put_line('找不到此人');  
  when others then
    null;
end;  

2.sql 属性

--sql 属性
--SQL%FOUND和SQL%NOTFOUND
begin
   update t set sal= &sal where empno= &no;
   if SQL%FOUND then
     dbms_output.put_line('更新完成');
   else
     dbms_output.put_line('没有找到数据');
   end if;
end;  


--为什么没有显示汉字信息?
declare
     v_sal number;
begin
     select sal into v_sal from emp where empno = 743;
     if (SQL%FOUND) then
        dbms_output.put_line('sal = ' || v_sal);
     else
        dbms_output.put_line('找不到此编号');
     end if;
end;

ORA-01403: no data found
ORA-06512: at line 4


SQL%NOTFOUND 与上面相反



--返回作用的计数:SQL%ROWCOUNT

begin
  update t set sal = 1 ;
  dbms_output.put_line('has updated ' || SQL%ROWCOUNT || ' rows');
end;

3. 输入一个部门号,按不同的部门进行加薪


--输入一个部门号,按不同的部门进行加薪
declare
  v_deptno emp.deptno%type;
begin
  v_deptno := &no;
  case v_deptno
    when 10 then
      update emp set comm = 100 where deptno = v_deptno;
    when 20 then
      update emp set comm = 80 where deptno = v_deptno;
    when 30 then
      update emp set comm = 50 where deptno = v_deptno;
    else
      dbms_output.put_line('不存此部门');
  end case;
end;  

4. 一次取多个值用select into

--一次取多个值用select into 
declare
    type emp_table_type is table of emp.ename%type index by binary_integer;
    emp_table emp_table_type;
begin
    select ename bulk collect into emp_table from emp;
    for i in 1..emp_table.count loop
      dbms_output.put_line(emp_table(i));
    end loop;
end;  


declare
    type emp_table_type is table of emp.ename%type;
    emp_table emp_table_type;
begin
    select ename bulk collect into emp_table from emp;
    for i in 1..emp_table.count loop
      dbms_output.put_line(emp_table(i));
    end loop;
end;  

5. 取出全部列

--取出全部列
declare
    type emp_table_type  is table of emp%rowtype;
    emp_table emp_table_type;
begin
    select * bulk collect into emp_table from emp;
    for i in 1..emp_table.count loop
       dbms_output.put_line('name: ' || emp_table(i).ename || '  sal: ' || emp_table(i).sal);
    end loop;
end;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA开发区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值