oracle测试程序,oracle简单存储过程测试
发布日期:2021-06-24 14:12:30 浏览次数:2 分类:技术文章

本文共 3351 字,大约阅读时间需要 11 分钟。

最近在复习下oracle的存储过程,写了个简单的例子。

create or replace procedure raise_comm(v_no emp.empno%type,

v_sale out emp.sale%type) as

begin

select sale into v_sale from emp where empno = v_no;

if v_sale is null then

raise_application_error(-20001, 'It is no sale for this employee');

end if;

exception

when no_data_found then

dbms_output.put_line('The employee is not exist');

when others then

dbms_output.put_line('Error No:'||SQLCODE);

dbms_output.put_line(SQLERRM);

end;

然后习惯性地在pl/sql下面的cmd window下测试,测试如下:

SQL>set serveroutput on;

SQL>variable g_sal number;

SQL>call raise_comm(7080,:g_sal);

Method called

g_sal

---------

看到结果当时愣了下,感觉有点不正常,我的异常信息怎么不见了,编号7080的记录不存在,不应该是这个结果,而是又换了个编号存在的继续测试,测试结果为:

SQL> call raise_comm(7821,:g_sal);

Method called

g_sal

---------

1250

我的异常信息真的不见了,而是换了种方式测试,找到存储过程,选中-->点击右键,选择Test

0818b9ca8b590ca3270a3433284dd417.png

然后可以看到系统自动生成的测试方法:

0818b9ca8b590ca3270a3433284dd417.png

在Variable下面的Value里面填值,我的一个是入参,一个是出参,只要v_no填值就OK了。先测试不存在的记录

0818b9ca8b590ca3270a3433284dd417.png

直接按F8,在DBMS Output页签下可以看到打印结果。

0818b9ca8b590ca3270a3433284dd417.png

在测试一个存在的正常数据。

0818b9ca8b590ca3270a3433284dd417.png

可以看到出参用黄颜色标记了。

在测试一个异常的数据。v_no填11,可以在DBMS Output页签下面看到Exception信息。

0818b9ca8b590ca3270a3433284dd417.png

看到Test测试正常,感觉在Cmd Window下面直接测试存储过程有点不靠谱,打印信息都不见了,仿照系统自动生成的脚本,自己写了个测试脚本。

declare

g_sal number;

begin

raise_comm(&v_no, :g_sal);

end;

在pl/sql的Cmd Window下运行:

SQL> start f:/saveFile/testsql.sql

6 /

The employee is not exist

PL/SQL procedure successfully completed

g_sal

---------

SQL> start f:/saveFile/testsql.sql

6 /

PL/SQL procedure successfully completed

g_sal

---------

980

SQL> start f:/saveFile/testsql.sql

6 /

Error No:-20001

ORA-20001: It is no sale for this employee

PL/SQL procedure successfully completed

g_sal

---------

可以看到DBMS Output的打印信息。

如果不想新建一个脚本,想直接在Cmd Window 下面看到打印结果,可以这样做:

SQL> set serveroutput on

SQL> declare

2 g_sal number;

3 begin

4 select sale into g_sal from emp where empno=&v_no;

5 if g_sal is null then

6 raise_application_error(-20001, 'It is no sale for this employee');

7 end if;

8 dbms_output.put_line('The employee is not exist');

9 dbms_output.put_line('g_sal='||g_sal);

10 exception

11 when no_data_found then

12 dbms_output.put_line('The employee is not exist');

13 when others then

14 dbms_output.put_line('Error No:'||SQLCODE);

15 dbms_output.put_line(SQLERRM);

16 end;

17 /

The employee is not exist

PL/SQL procedure successfully completed

SQL>

SQL> declare

2 g_sal number;

3 begin

4 select sale into g_sal from emp where empno=&v_no;

5 if g_sal is null then

6 raise_application_error(-20001, 'It is no sale for this employee');

7 end if;

8 dbms_output.put_line('The employee is not exist');

9 dbms_output.put_line('g_sal='||g_sal);

10 exception

11 when no_data_found then

12 dbms_output.put_line('The employee is not exist');

13 when others then

14 dbms_output.put_line('Error No:'||SQLCODE);

15 dbms_output.put_line(SQLERRM);

16 end;

17 /

The employee is not exist

g_sal=980

PL/SQL procedure successfully completed

SQL>

SQL> declare

2 g_sal number;

3 begin

4 select sale into g_sal from emp where empno=&v_no;

5 if g_sal is null then

6 raise_application_error(-20001, 'It is no sale for this employee');

7 end if;

8 dbms_output.put_line('The employee is not exist');

9 dbms_output.put_line('g_sal='||g_sal);

10 exception

11 when no_data_found then

12 dbms_output.put_line('The employee is not exist');

13 when others then

14 dbms_output.put_line('Error No:'||SQLCODE);

15 dbms_output.put_line(SQLERRM);

16 end;

17 /

Error No:-20001

ORA-20001: It is no sale for this employee

PL/SQL procedure successfully completed

全文完。

转载地址:https://blog.csdn.net/weixin_33326218/article/details/116485929 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:oracle crs ocr 恢复,Oracle 无备份情况恢复ocr和olr
下一篇:oracle一写入数据就被锁死,Oracle数据库被恶意锁死怎么办?英方i2CDP来恢复

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月06日 13时43分13秒