oracle函数、存储过程、游标

本文详细介绍了PL/SQL中的函数定义与使用方法,并深入探讨了游标的使用技巧,包括显式游标、更新数据及REF游标等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、函数
     带in参数并其中用到游标的函数:
create or replace function fx_get_czxmdm(bmwyh in number) return varchar2 is
  Result varchar2(500);
  Result_Temp varchar2(500);
  -----游标开始-----(这之间也可以不是游标,可以是函数、存储过程、sql语句等)---
      begin
        DECLARE
          CURSOR c1 IS
            select ajj005.czxm005 as czxm005
              from USEEASY_TZZYSYS_015RYKS ajj015,
                   USEEASY_TZZYSYS_005CZXM ajj005,
                   USEEASY_TZZYSYS_021DYXM ajj021
             where ajj015.bmwyh015 = ajj021.bmwyh021 and
                   ajj005.czxm005 = ajj021.czxm021 and ajj015.bmwyh015 = bmwyh  order by czxm005;

          emp_record c1% ROWTYPE;
        BEGIN
          Result_Temp:='';
          FOR emp_record IN c1 LOOP
            -- implicit open and implicit fetch occur

            Result_Temp := Result_Temp||emp_record.czxm005||'、';

          END LOOP;
          -- implicit close occurs
        END;
   -------游标结束------
  if Result_Temp is null then
    Result := '';
  else
    Result := substr(Result_Temp,0,length(Result_Temp)-1);
  end if;
  return(Result);
end fx_get_czxmdm;
==============================
2、存储过程
     带多个in参数的存储过程
 create or replace procedure copy_tk(newtkbh in number, oldtkbh in number) is
 begin
      insert into useeasy_tzzysys_053st
              (stbh053,   tkbh053,   stfl053,   stlr053,   stlx053,   a053,   b053,   c053,   d053,   stda053,   stnd053,   stbz053)
      select
             ajj_053st.nextval,  newtkbh,  stfl053,  stlr053, tlx053,  a053,  b053,  c053,   d053,  stda053,  stnd053,  stbz053
      from useeasy_tzzysys_053st
     where tkbh053 = oldtkbh;
 end copy_tk;
===================
3、游标=  =显式游标
1、必须在声明部分显式声明
2、用于处理返回多行的查询结果集,方便用户逐行处理数据
3、游标属性
1)%rowcount,返回当前游标对应的行号
2)%found,如果fetch成功,则返回true,否则返回false
3)%notfound,如果fetch失败,则返回true,否则返回false
4)%isopen,如果游标打开,则返回true,否则返回false

declare
  --1 声明游标
  cursor mycursor
  is
  select ename,sal from emp;  
  e_name emp.ename%type;
  e_sal emp.sal%type;
begin
  --2 打开游标
  if(mycursor%isopen=false) then
    open mycursor;
  end if;
  --3 循环遍历游标
  loop
    --4 提取当前游标的值
    fetch mycursor into e_name,e_sal;
    --退出循环
    exit when mycursor%notfound;
    if(e_sal>2000) then    
      dbms_output.put_line(mycursor%rowcount||'-'||e_name||'-'||e_sal);
    end if;
  end loop;
  --5 关闭游标
  close mycursor;
end; 
------------------------------------------------------------------------------------------------------------- 
    使用游标更新数据
     1)使用 select..for update nowait; 给游标加锁
     2)使用 where current of 游标名; 限定修改当前游标所在行
--更新员工薪水
--1500以下,加500
--2500以下,加300
--其他,加200
declare
  cursor mycursor
  is
  select * from emp for update nowait;
  money number;
begin
  for r in mycursor
  loop
    if(r.sal<=1500) then
      money:=500;
    elsif(r.sal<=2500) then
      money:=300;
    else
      money:=200;
    end if;
    update emp set sal=sal+money where current of mycursor;
  end loop;
end;
--------------------------------------------------------------------------------------
REF游标
1、用于运行时才能确定的查询结果集,一般与动态SQL连用
2、静态SQL,编译时已经明确的SQL语句(效率高、灵活性低)
select * from emp;
delete from emp where empno=7369;
3、动态SQL,运行时才能确定的SQL语句(效率低,灵活性高)
语法:execute immediate '动态SQL';
1)动态SQL用于在PL/SQL块中执行DDL语句
begin
  execute immediate
  'create table test
  (tid number,
  tname varchar2(10))';
end;
2)动态SQL用于执行运行时才能确定的SQL语句
declare
  e_name emp.ename%type;
  e_no emp.empno%type;
begin
  e_no:='&e_no';
  execute immediate
  'select ename from emp where empno=:e_no'--占位符
  into e_name --将查询的值赋值给变量
  using e_no;--给占位符绑定值
  dbms_output.put_line(e_name);
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值