您的位置:首页 > 数据库 > Oracle

oracle函数、存储过程、游标

2013-05-28 15:28 211 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: