Oracle存储过程笔记
Oracle之存储过程
存储过程定义,变量声明,获取数值,输出方法
declare
--给一个变量申明和赋值
v_age 类型 := 值;
--这个变量被称为引用变量类型,就是说这个变量的类型和表中的类型相同,一般推荐这种使用方法
v_name [表名].[字段名]%Type;
--相当于java方法
begin
--给v_name 赋值,这里根据自己的表去改
select person_name into v_name from [表名] where person_id = '000820';
--输出语句, || 是拼接的意思
dbms_output.put_line('姓名: ' || v_name || ' 年龄: ' || v_age);
end;
记录型变量(不推荐使用)
declare
--记录型变量声明
v_row [表名]%rowtype;
begin
--要查出一整条记录,所以用*
select * into v_row from [表名] where person_id = '000820';
--输出时,当成对象去.属性名
dbms_output.put_line(v_row.person_id || v_row.person_name);
end;
if分支
declare
v_depId [表名].[字段名]%type;
begin
select depart_id into v_depId from [表名] where person_id = '000820';
--分支开始
if v_depId = '41' then
dems_output.put_line('部门是收费室');
--一定要注意这个elsIf,没有e,不是else
elsIf v_depId = '48' then
dems_output.put_line('部门是神经疾病科');
end if;
end;
循环
declare
v_cnt integer := 0;
begin
--循环开始
loop
--退出循环条件,我想打印0-30嘛
exit when v_cont > 30
--输出
dems_output.put_line(v_cnt);
--变量更新
v_cnt := v_cnt + 1;
--循环结束
end loop;
end;
游标
declare
--定义名称,编号变量
v_name, v_id
--创建游标
cursor c_row is select [字段名],[字段名]... from [表名];
begin
--打开游标
open c_row;
loop
--取出数值
fetch c_row into v_name, v_id;
--退出条件
exit when c_row%notfound;
dems_output.put_line(v_name || ' ' || v_id);
end loop;
--关闭游标
close c_row;
end;