ORACLE存储过程,循环语法和游标
发布时间:2020-12-31 16:35:19 所属栏目:站长百科 来源:网络整理
导读:1、定义 所谓存储过程(Stored Procedure),就是一组用于完成特定数据库功能的SQL语句集,该SQL语句集经过 编译后存储在数据库系统中。在使用时候,用户通过指定已经定义的存储过程名字并给出相应的存储过程参数 来调用并执行它,从而完成一个或一系列的数据
|
3. loop declare
var number :=1;
begin
loop
dbms_output.put_line(var);
exit when var > 1;
var := var + 1;
end loop;
end;
语法: cursor 游标名 [ (参数名? ? 数据类型,参数名 ? 数据类型,...)] ?is select 语句; 例: cursor c1 is select ename from emp; 游标的属性: ? ? ? ? ? ? ? ? %isopen????? ? 是否打开????? ? boolean类型 ????????????? ? %rowcount ? ?影响的行数 ? ?不是总行数,例如总数100,已经取了10条,那么这个数为10 ????????????? ? %found ? ? ? ? 是否找到????????boolean类型 ????????????? ? %notfound ? ?是否没找到? ? ?boolean类型 简单实例1: declare
cursor p is select t.name,t.money from test_procedure t;
pname test_procedure.name%type;
pmoney test_procedure.money%type;
begin
--使用前要开启光标
open p;
loop
--取当前记录
fetch p into pname,pmoney;
exit when p%notfound;
dbms_output.put_line(pname||‘薪水是‘||pmoney);
end loop;
--结束要关闭光标
close p;
end;
简单实例2: declare
cursor p is select * from test_procedure t;
var test_procedure%rowtype;
begin
--使用前要开启光标
open p;
loop
--取当前记录
fetch p into var;
exit when p%notfound;
dbms_output.put_line(var.name||‘薪水是‘||var.money);
end loop;
--结束要关闭光标
close p;
end;
简单实例3: declare ? cursor p is select * from test_procedure t;?? ? var test_procedure%rowtype; begin ? --使用前要开启光标 ? open p; ? ?loop ? ? ? ?--取当前记录 ? ? ? ?fetch p into var;? ? ? ? ? ? ? ?if var.job = ‘后端工程师‘? ? ? ? ? ?then update test_procedure set money = money+10000 where name = var.name; ? ? ? ?elsif var.job = ‘前端工程师‘ ? ? ? ? ?then update test_procedure set money = money+5000 where name = var.name; ? ? ? ?else ? ? ? ? ?update test_procedure set money = money+50000 where name = var.name; ? ? ? ?end if; ? ? exit when p%notfound;? ?? ? ? dbms_output.put_line(var.name||‘薪水是‘||var.money);?? ? end loop;?? ? --结束要关闭光标 ? close p; ? --提交事务 ? commit;?? ? dbms_output.put_line(‘完成‘); end; 带参数的游标用法 --查询某个部门员工的姓名
declare
--形参
--在游标定义一个参数
cursor temp(pjob varchar2) is select name from test_procedure where job = pjob;
pname test_procedure.name%type;
begin
--传参
--开启游标传递参数
open temp(‘后端工程师‘);
loop
fetch temp into pname;
exit when temp%notfound;
dbms_output.put_line(pname);
end loop;
close temp;
end;
(编辑:清远站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

