目录
0、执行以下存过需要的建表语句
执行一下存过需要的建表语句
create table dept30 as select * from dept;
create table DEPT30_TEMP as select * from dept;
1、简单
========================================================定义变量
psal number(7,2);
my_name emp.ename%type; --该表中的字段的类型为当前变量的类型
emp_rec emp%rowtype; --记录型变量 一行一条记录
var1 char(15); --定义var1为char 15个长度
married boolean:=true --赋默认值为true
total number:=0; --赋默认值为0
v_table varchar(4000); --定义变量,需要指定长度
========================================================pro_001
create or replace procedure pro_001
is
begin
dbms_output.put_line('hello world');
end;
========================================================pro_014
CREATE OR REPLACE PROCEDURE pro_014
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
step INTEGER := 0;
cTime DATE;
v_table varchar(4000); --定义变量,需要指定长度
m_name emp.ename%type;
m_sal emp.sal%type;
BEGIN
SELECT ename, sal into m_name, m_sal FROM emp e where e.empno = '7369';
dbms_output.put_line(m_name || ' 的薪水是 '|| m_sal);
select count(1) as num into c from dept30;
commit;
msg:='执行 pro_014 成功 ';
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_002 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_014;
2、带输入和输出存过
CREATE OR REPLACE PROCEDURE pro_002
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
step INTEGER := 0;
cTime DATE;
v_table varchar(4000); --定义变量,需要指定长度
BEGIN
dbms_output.put_line('a='||a );
dbms_output.put_line('b='||b );
select count(1) as num into c from dept30;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_002 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_002;
3、查询多列的输出
CREATE OR REPLACE PROCEDURE pro_003
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar,
msg1 out varchar,
msg2 out varchar
)
IS
step INTEGER := 0;
cTime DATE;
v_table varchar(4000); --定义变量,需要指定长度
BEGIN
dbms_output.put_line('a='||a );
dbms_output.put_line('b='||b );
c:='1';
select DEPTNO ,DNAME into msg1,msg2 from dept30 where deptno='62';
msg:='执行 pro_003 成功 ';
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_003 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_003;
4、带if 的存过
========================================================if语句结构
--if语句
if 条件 then 语句1;
语句2;
end if;
if 条件 then 语句1;
esle 语句2;
end if;
if 条件 then 语句1;
elseif 语句2 then 语句;
else 语句3
end if;
CREATE OR REPLACE PROCEDURE pro_019
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
step INTEGER := 0;
cTime DATE;
v_table varchar(4000); --定义变量,需要指定长度
total number:=0;
i dept30%rowtype; --记录型变量
count_num NUMBER :=0;
BEGIN
select /*+parallel(16) */ count(1) into count_num from DEPT30_TEMP;
if count_num < 50000 then
dbms_output.put_line('50000=====> '|| count_num);
else
dbms_output.put_line('count_num=====> '|| count_num);
end if;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_019 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_019;
========================================================
CREATE OR REPLACE PROCEDURE pro_004
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar,
msg1 out varchar,
msg2 out varchar,
msg3 out varchar
)
IS
step INTEGER := 0;
cTime DATE;
v_table varchar(4000); --定义变量,需要指定长度
BEGIN
IF a=123 THEN
BEGIN
select DEPTNO ,DNAME into msg1 , msg2 from dept30 where deptno='62';
msg:='执行 pro_004 成功 ';
END;
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_004 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_004;
5、带循环的存过
循环格式1
CREATE OR REPLACE PROCEDURE pro_005
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar,
msg1 out varchar,
msg2 out varchar,
msg3 out varchar
)
IS
step INTEGER := 0;
cTime DATE;
v_table varchar(4000); --定义变量,需要指定长度
counum number:=0;
BEGIN
WHILE a=123 LOOP
BEGIN
select DEPTNO ,DNAME into msg1 , msg2 from dept30 where deptno='62';
msg:='执行 pro_005 成功 ';
counum:=counum+1;
dbms_output.put_line(counum);
exit when counum > 100;
END;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_005 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_005;
循环格式2
============================================== loop2
CREATE OR REPLACE PROCEDURE loop2
is
BEGIN
-- 打印 1-100的自然数,当打印到50时,跳出循环,输出打印结束
-- goto 跳出循环 到指定位置 exit相当于break;
for c in 1 .. 10 loop
dbms_output.put_line(c);
if c = 5 then
goto lable;
end if;
end loop;
<<lable>>
dbms_output.put_line('打印结束 这个是lable');
dbms_output.put_line(' ');
for c in 1 .. 10 loop
dbms_output.put_line(c);
if c = 5 then
dbms_output.put_line('打印结束'); exit;
end if;
end loop;
END;
循环格式3
============================================== loop3
CREATE OR REPLACE
PROCEDURE "loop"
AS
/*
循环1-100 用三种方式
*/
v_i NUMBER := 1;
v_j NUMBER := 1;
BEGIN
-- 方式1
loop
dbms_output.put_line(v_i);
v_i := v_i + 1;
exit when v_i > 100;
end loop;
-- 方式2
while v_j <= 100 loop
dbms_output.put_line(v_j);
v_j := v_j + 1;
end loop;
-- 方式3 正序输出
for c in 1 .. 100 loop
dbms_output.put_line(c);
end loop;
-- 方式3 倒序输出
for c in reverse 1 .. 100 loop
dbms_output.put_line(c);
end loop;
END;
6、游标
简单
==========================================游标用法
SQL%ROWCOUNT 整型 代表DML语句成功执行的数据行数
SQL%FOUND 布尔型 值为TRUE代表插入、删除、更新或单行查询操作成功
SQL%NOTFOUND 布尔型 与SQL%FOUND属性返回值相反
SQL%ISOPEN 布尔型 DML执行过程中为真,结束后为假
结论:一般来说 '批量处理' 的速度要最好,'隐式游标' 的次之,'单条处理' 的最差
说明:若有兴趣,可以在数据量多的表里面,分别尝试下列三种写法,并打印时间,用作比较
1. 批量处理
open 游标;
loop
fetch 游标 bulk collect
into 集合变量(也就是 table 类型哦) limit 数值; -- 一般 500 左右
exit when 条件; -- cursor.count = 0
逻辑处理;
end loop;
close 游标;
2. 隐式游标
for x in (sql 语句) loop
逻辑处理;
end loop;
3. 单条处理
open 游标;
loop
fetch 游标
into 变量;
exit when 条件;
逻辑处理;
end loop;
close 游标;
建表
==========================================建表
-- 测试基础数据
create table stu_info (
id number(3),
name varchar2(30),
sex varchar2(3)
);
insert into stu_info(id, name, sex) values (1, '小游子', '女');
insert into stu_info(id, name, sex) values (2, '小优子', '男');
commit;
用法1
==========================================玩法1
CREATE OR REPLACE PROCEDURE pro_006
(
msg out varchar
)
IS
-- 1 定义游标
cursor cur_stu_info is select * from stu_info;
-- 2 定义一个v_stu_info 变量 类型是 表类型cur_stu_info
v_stu_info cur_stu_info%rowtype;
BEGIN
-- 3 开启游标
open cur_stu_info;
-- 4 获取一行数据
fetch cur_stu_info into v_stu_info;
-- 5输出
dbms_output.put_line(v_stu_info.id || ' : ' || v_stu_info.name);
-- 6关闭游标
close cur_stu_info;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_006 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_006;
用法2
==========================================玩法2
CREATE OR REPLACE PROCEDURE pro_007
(
msg out varchar
) IS
-- 1 定义游标
cursor cur_stu_info is select * from stu_info;
-- 2 定义一个v_stu_info 变量 类型是 表类型cur_stu_info
v_stu_info cur_stu_info%rowtype;
BEGIN
open cur_stu_info;
loop
fetch cur_stu_info into v_stu_info;
exit when cur_stu_info%notfound; -- 没有找到则退出
dbms_output.put_line(v_stu_info.id || '|@| ' || v_stu_info.name);
end loop;
close cur_stu_info;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg := '执行 pro_007 失败' || 'OraError = ' || SQLCODE || SQLERRM(SQLCODE);
if cur_stu_info%isopen then
close cur_stu_info;
end if;
END pro_007;
用法3
==========================================玩法3
CREATE OR REPLACE PROCEDURE pro_008
(
msg out varchar
) IS
cursor cur_stu_info is select id,name from stu_info; -- 1 定义游标
v_stu_info cur_stu_info%rowtype; -- 2 定义一个v_stu_info 变量 类型是 表类型cur_stu_info
v_stu_name stu_info.name%type; -- 定义 和表 stu_info 的 name 类型
v_stu_id stu_info.id%type; -- 定义 和表 stu_info 的 id 类型
BEGIN
open cur_stu_info;
loop
fetch cur_stu_info into v_stu_id,v_stu_name; -- 将数据装入
exit when cur_stu_info%notfound; -- 没有找到则退出
dbms_output.put_line(v_stu_id || '|@| ' || v_stu_name);
end loop;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg := '执行 pro_008 失败' || 'OraError = ' || SQLCODE || SQLERRM(SQLCODE);
if cur_stu_info%isopen then
close cur_stu_info;
end if;
END pro_008;
用法4-重要
==========================================玩法4
15-1:使用标量变量接收游标数据
DECLARE
CURSOR emp_cursor IS
SELECT ename,job,sal FROM emp WHERE deptno=&dno;
v_ename emp.ename%TYPE;
v_sal emp.sal%TYPE;
v_job emp.job%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_ename,v_job,v_sal;
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('姓名:'||v_ename||',岗位:'||v_job||',工资:'||v_sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-2:使用pl/sql纪录变量接收游标数据
DECLARE
CURSOR emp_cursor IS
SELECT ename,sal FROM emp ORDER BY sal DESC;
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND OR emp_cursor%ROWCOUNT>&n;
dbms_output.put_line('姓名:'||emp_record.ename||
',工资:'||emp_record.sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-3:使用pl/sql集合变量接收游标数据
DECLARE
CURSOR emp_cursor IS SELECT ename,sal FROM emp
WHERE lower(job)=lower('&job');
TYPE emp_table_type IS TABLE OF emp_cursor%ROWTYPE
INDEX BY BINARY_INTEGER;
emp_table emp_table_type;
i INT;
BEGIN
OPEN emp_cursor;
LOOP
i:=emp_cursor%ROWCOUNT+1;
FETCH emp_cursor INTO emp_table(i);
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('姓名:'||emp_table(i).ename||
',工资:'||emp_table(i).sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-4:在游标for循环中引用已定义游标
DECLARE
CURSOR emp_cursor IS SELECT ename,hiredate FROM emp ORDER BY hiredate DESC;
BEGIN
FOR emp_record IN emp_cursor LOOP
dbms_output.put_line('姓名:'||emp_record.ename ||',工作日期:'||emp_record.hiredate);
EXIT WHEN emp_cursor%ROWCOUNT=&n;
END LOOP;
END;
/
15-5:在游标for循环中直接引用子查询
BEGIN
FOR emp_record IN (SELECT ename,hiredate,rownum FROM emp ORDER BY hiredate) LOOP
dbms_output.put_line('姓名:'||emp_record.ename
||',工作日期:'||emp_record.hiredate);
EXIT WHEN emp_record.rownum=&n;
END LOOP;
END;
/
15-6:参数游标
DECLARE
CURSOR emp_cursor(dno NUMBER) IS
SELECT ename,job FROM emp WHERE deptno=dno;
BEGIN
FOR emp_record IN emp_cursor(&dno) LOOP
dbms_output.put_line('姓名:'||emp_record.ename
||',岗位:'||emp_record.job);
END LOOP;
END;
/
15-7:更新游标行
DECLARE
CURSOR emp_cursor IS SELECT ename,sal,deptno FROM emp FOR UPDATE;
dno INT:=&no;
BEGIN
FOR emp_record IN emp_cursor LOOP
IF emp_record.deptno=dno THEN
dbms_output.put_line('姓名:'||emp_record.ename
||',原工资:'||emp_record.sal);
UPDATE emp SET sal=sal*1.1 WHERE CURRENT OF emp_cursor;
commit;
END IF;
END LOOP;
END;
/
15-8:删除游标行
DECLARE
CURSOR emp_cursor IS SELECT ename FROM emp FOR UPDATE;
name VARCHAR2(10):=lower('&name');
BEGIN
FOR emp_record IN emp_cursor LOOP
IF lower(emp_record.ename)=name THEN
DELETE FROM emp WHERE CURRENT OF emp_cursor;
commit;
ELSE
dbms_output.put_line('姓名:'||emp_record.ename);
END IF;
END LOOP;
END;
/
15-9:使用of子句在特定表上加行共享锁.
DECLARE
CURSOR emp_cursor IS
SELECT a.dname,b.ename FROM dept a JOIN emp b ON a.deptno=b.deptno
FOR UPDATE OF b.deptno;
name VARCHAR2(10):=LOWER('&name');
BEGIN
FOR emp_record IN emp_cursor LOOP
IF LOWER(emp_record.dname)=name THEN
dbms_output.put_line('姓名:'||emp_record.ename);
DELETE FROM emp WHERE CURRENT OF emp_cursor;
commit;
END IF;
END LOOP;
END;
/
15-10:使用无返回类型的游标变量
DECLARE
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
v1 NUMBER(6);
v2 VARCHAR2(10);
BEGIN
OPEN ref_cursor FOR
SELECT &col1 col1,&col2 col2 FROM &table WHERE &cond;
LOOP
FETCH ref_cursor INTO v1,v2;
EXIT WHEN ref_cursor%NOTFOUND;
dbms_output.put_line('col1='||v1||',col2='||v2);
END LOOP;
CLOSE ref_cursor;
END;
/
15-11:使用有返回类型的游标变量
DECLARE
TYPE emp_cursor_type IS REF CURSOR RETURN emp%ROWTYPE;
emp_cursor emp_cursor_type;
emp_record emp%ROWTYPE;
BEGIN
OPEN emp_cursor FOR SELECT * FROM emp WHERE deptno=&dno;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('姓名:'||emp_record.ename|| ',工资:'||emp_record.sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-12:使用fetch...bulk collect提取所有数据
DECLARE
CURSOR emp_cursor IS SELECT * FROM emp WHERE LOWER(job)=LOWER('&job');
TYPE emp_table_type IS TABLE OF emp%ROWTYPE;
emp_table emp_table_type;
BEGIN
OPEN emp_cursor;
FETCH emp_cursor BULK COLLECT INTO emp_table;
CLOSE emp_cursor;
FOR i IN 1.. emp_table.COUNT LOOP
dbms_output.put_line('姓名:'||emp_table(i).ename ||',工资:'||emp_table(i).sal);
END LOOP;
END;
/
15-13:使用limit子句限制提取行数 (重要)
DECLARE
CURSOR emp_cursor IS SELECT * FROM emp;
TYPE emp_array_type IS VARRAY(5) OF emp%ROWTYPE;
emp_array emp_array_type;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor BULK COLLECT INTO emp_array LIMIT 10000;
FOR i IN 1..emp_array.COUNT LOOP
dbms_output.put_line('姓名'||emp_array(i).ename ||',工资'||emp_array(i).sal);
END LOOP;
EXIT WHEN emp_cursor%NOTFOUND;
END LOOP;
CLOSE emp_cursor;
END;
/
批量游标4--重要
========================类似的还有如下
-------批量新增---------
DECLARE
CURSOR cur IS
SELECT a.XH, a.ZYH, a.BJH, a.XM, a.XBM, a.SFZJH, a.CSRQ
FROM xsxxgl_xsjbxx a
WHERE not exists (SELECT 1 FROM zs_xsxx2 b WHERE a.xh = b.xsid); --定义游标
TYPE rec IS TABLE OF cur%ROWTYPE; --定义类型
recs rec;
BEGIN
OPEN cur; --打开游标
WHILE (TRUE) LOOP
--循环条件
FETCH cur BULK COLLECT --取游标里的值
INTO recs LIMIT 100; --提交条件,每100条提交
FORALL i IN 1 .. recs.COUNT --这个是个计数器,用来确保提交的条数,也是循环
INSERT INTO zs_xsxx2
(XSID, BDZY, BJDM, XM, XB, SFZH, CSRQ)
VALUES
(recs(i).XH,
recs(i).ZYH,
recs(i).BJH,
recs(i).XM,
recs(i).XBM,
recs(i).SFZJH,
recs(i).CSRQ); --插入目标表
COMMIT; --提交
EXIT WHEN cur%NOTFOUND; --游标结束条件
END LOOP; --停止循环
CLOSE cur; --关闭游标
END;
========================
15-14:使用cursor表达式
DECLARE
CURSOR dept_cursor(no NUMBER) IS
SELECT a.dname,CURSOR(SELECT * FROM emp
WHERE deptno=a.deptno)
FROM dept a WHERE a.deptno=no;
TYPE ref_cursor_type IS REF CURSOR;
emp_cursor ref_cursor_type;
emp_record emp%ROWTYPE;
v_dname dept.dname%TYPE;
BEGIN
OPEN dept_cursor(&dno);
LOOP
FETCH dept_cursor INTO v_dname,emp_cursor;
EXIT WHEN dept_cursor%NOTFOUND;
dbms_output.put_line('部门名:'||v_dname);
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('雇员名:'||emp_record.ename ||',岗位:'||emp_record.job);
END LOOP;
END LOOP;
CLOSE dept_cursor;
END;
==========================================玩法5
CREATE OR REPLACE PROCEDURE pro_012
(
msg out varchar
) IS
cursor cur_stu_info is select * from stu_info;
TYPE stu_info_type IS TABLE OF stu_info%ROWTYPE;
stu_info_table stu_info_type;
insert_string varchar(4000);
BEGIN
/*
1、清空表
2、将stu_info 中的数据 转移到 stu_info_temp
*/
insert_string:='insert into stu_info_temp(id,name,sex,timestr) values(:1,:2,:3,:4)';
execute immediate 'truncate table stu_info_temp ';
open cur_stu_info;
loop
FETCH cur_stu_info BULK COLLECT INTO stu_info_table limit 1;
FOR i IN 1.. stu_info_table.COUNT LOOP
execute immediate insert_string using stu_info_table(i).id,stu_info_table(i).name,stu_info_table(i).sex,sysdate;
-- commit;
END LOOP;
commit;
exit when cur_stu_info%notfound;
end loop;
CLOSE cur_stu_info;
msg:='exec success ';
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg := '执行 pro_012 失败' || 'OraError = ' || SQLCODE || SQLERRM(SQLCODE);
if cur_stu_info%isopen then
close cur_stu_info;
end if;
END pro_012;
7、默认游标
======================================================默认游标名:'SQL'
declare
v_count number;
begin
insert into stu_info (id, name, sex) values (3, '瑶瑶', '女');
if sql%found then
dbms_output.put_line('插入成功!');
end if;
update stu_info t set t.name = '悠悠' where t.id = 3;
if sql%found then
dbms_output.put_line('更新成功!');
end if;
delete from stu_info t where t.id = 3;
if sql%found then
dbms_output.put_line('删除成功!');
end if;
select count(1) into v_count from stu_info t;
if sql%found then
dbms_output.put_line('总记录为: ' || v_count);
end if;
if sql%isopen then
dbms_output.put_line('不可能的,永远不可能走这一步');
else
dbms_output.put_line('系统已自动关闭游标');
end if;
end;
9、使用using 传参
====================================== 定义为 :1 通过 using 来传入a的参数值
CREATE OR REPLACE PROCEDURE pro_018
(
a IN varchar, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
step INTEGER := 0;
v_sql varchar(4000);
BEGIN
-- 定义为 :1 通过 using 来传入a的参数值
v_sql := 'update EMP_0407 set SAL = 7000 where EMPNO = :1 ';
execute immediate v_sql using a;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_018 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_018;
========================================== pro_020
--- 在 pro_020 调用 pro_019 ,并使用using 来入参
CREATE OR REPLACE PROCEDURE pro_020
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
v_sql varchar2(4000);
v_prcname varchar2(4000);
BEGIN
v_prcname :='pro_019';
v_sql := 'begin ' || v_prcname || '(:1,:2,:3,:4); END;';
dbms_output.put_line('v_sql=====> '|| v_sql);
execute immediate v_sql using
in a,
in b,
out c,
out msg;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_020 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_020;
11、异常
捕获到其他异常 WHEN OTHERS THEN
========================================= WHEN OTHERS THEN
CREATE OR REPLACE PROCEDURE pro_020
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
v_sql varchar2(4000);
v_prcname varchar2(4000);
BEGIN
v_prcname :='pro_019';
v_sql := 'begin ' || v_prcname || '(:1,:2,:3,:4); END;';
dbms_output.put_line('v_sql=====> '|| v_sql);
execute immediate v_sql using
in a,
in b,
out c,
out msg;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_020 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_020;
捕获自定义异常
========================================= WHEN OTHERS THEN
CREATE OR REPLACE PROCEDURE pro_021
(
a IN NUMBER, --定义输入
b IN date, --定义时间输入
c out number, --定义输出
msg out varchar
)
IS
v_sql varchar2(4000);
v_prcname varchar2(4000);
USER_RUNTIME_EXCEPTION EXCEPTION; -- 定义一个异常类型
BEGIN
v_prcname :='pro_019';
v_sql := 'begin ' || v_prcname || '(:1,:2,:3,:4); END;';
dbms_output.put_line('v_sql=====> '|| v_sql);
RAISE USER_RUNTIME_EXCEPTION; -- 自己抛出异常
commit;
EXCEPTION
WHEN USER_RUNTIME_EXCEPTION THEN --捕获异常
ROLLBACK;
msg:='自定义异常 USER_RUNTIME_EXCEPTION ';
WHEN OTHERS THEN
ROLLBACK;
msg:='执行 pro_021 失败'|| 'OraError = '|| SQLCODE || SQLERRM(SQLCODE);
END pro_021;