Oracle笔记 十、PL/SQL存储过程

简介:
--create or replace 创建或替换,如果存在就替换,不存在就创建
create or replace procedure p
is
  cursor c
  is
  select * from dept2 for update;
begin
  for row_record in c loop
    if (row_record.deptno = 30) then
      update dept2 set dname = substr(dname, 0, length(dname) - 3) where current of c;
    end if;
  end loop;
end;
 
exec p;
 
begin
p;
end;
 
--带参存储过程
--in 输入参数,不带in out 默认输入参数
--out 输出参数
--in out 同时带的是输入输入参数
create or replace procedure p2(
       a in number,
       b number,
       s_result out number,
       s_temp in out number
  )
is
begin
  if (a > b) then
    s_result := a;
  else
    s_result := b;
  end if;
  s_temp := s_temp + 3;
end;
 
--调用存储过程
declare
  v_a number := 4;
  v_b number := 6;
  v_result number;
  v_temp number := 5;
begin
  p2(v_a, v_b, v_result, v_temp);
  dbms_output.put_line(v_a);
  dbms_output.put_line(v_b);
  dbms_output.put_line(v_result);
  dbms_output.put_line(v_temp);
end;
 
---删除一个表的过程
create or replace procedure drop_table(tname varchar2)
as
  total int := 0;
begin
     select count(*) into total from user_tables 
            where table_name = upper(tname);
     if total >= 1 then
        execute immediate 'drop table '||tname; --此处必须用动态sql
     end if;
end;
select * from user_tables;
 
--递归存储过程
create or replace procedure pro_emp(sEmpno emp.empno%type, sLevel integer)
is
       cursor c is select * from emp where mgr = sEmpno;
       prefixStr varchar(255);
begin
  for i in 1..sLevel loop
    prefixStr := prefixStr || '----';
  end loop;
  
  for row_data in c loop
    dbms_output.put_line(prefixStr || row_data.ename);
    pro_emp(row_data.empno, sLevel + 1);
  end loop;
end;
 
select * from emp;
begin
  pro_emp(7839, 0);
end;





本文转自hoojo博客园博客,原文链接:http://www.cnblogs.com/hoojo/archive/2011/05/03/2035388.html,如需转载请自行联系原作者
目录
相关文章
|
26天前
|
存储 SQL 数据库
sql serve存储过程
sql serve存储过程
14 0
|
9天前
|
SQL 存储 关系型数据库
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-2
【4月更文挑战第5天】两阶段提交是为确保`redo log`和`binlog`逻辑一致,避免数据不一致。若先写`redo log`, crash后数据可能丢失,导致恢复后状态错误;若先写`binlog`,crash则可能导致重复事务,影响数据库一致性。一天一备相较于一周一备,能缩短“最长恢复时间”,但需权衡额外的存储成本。
15 1
|
24天前
|
Oracle 关系型数据库
oracle基本笔记整理及案例分析2
oracle基本笔记整理及案例分析2
12 0
|
24天前
|
Oracle 关系型数据库
oracle基本笔记整理及案例分析1
oracle基本笔记整理及案例分析1
16 0
|
24天前
|
SQL Oracle 关系型数据库
oracle笔记整理2
oracle笔记整理2
11 0
|
24天前
|
SQL Oracle 关系型数据库
oracle基本笔记整理
oracle基本笔记整理
12 0
|
1月前
|
SQL Oracle 关系型数据库
Oracle系列十一:PL/SQL
Oracle系列十一:PL/SQL
|
1月前
|
SQL Oracle 关系型数据库
Oracle系列之八:SQL查询
Oracle系列之八:SQL查询
|
6天前
|
SQL Oracle 关系型数据库
【Oracle】玩转Oracle数据库(一):装上去,飞起来!
【Oracle】玩转Oracle数据库(一):装上去,飞起来!
41 7

推荐镜像

更多