Oracle新建存储过程和程序包


建存储过程之前需要新增一张jdbc_student表,接下来用的到

新增一个简单的存储过程
create or replace procedure proc_jdbc_student_add(name in varchar2, age in number)
as begin
insert into jdbc_student (name, age) values (name, age);
end proc_jdbc_student_add;
调用存储过程
exec proc_jdbc_student_add('user4',28) 新增一个有返回值的存储过程 CREATE PROCEDURE proc_jdbc_student_outpara (inParam in varchar2, address out varchar2) as begin select address into address from jdbc_student where name = inParam; end proc_jdbc_student_outpara;
我也不会直接调用,我是用SQLdevloper自带的测试工具测的
新增存储过程后,右键存储过程可以执行或debug执行,只需要输入参数即可

新增程序包

create or replace package test_pkg is --test_pkg为包名
procedure showMessage; --声明一个过程
function myAdd(x in number,y in number) return number; --声明函数
function of_stop_clinic_dict(in_item_code in varchar2, out_message out varchar2) return integer; -- 声明一个有返回值的函数
end test_pkg;

--主体
create or replace package body test_pkg is --包名必须一致

procedure showMessage is --实现规范中的过程
begin
dbms_output.put_line('创建一个简单的包!'); --打印字符串用单引号括起来
end showMessage;

function myAdd(x in number,y in number) --实现函数
return number is
mySum number:=1;
begin
mySum:=x+y;
return mySum;
end myAdd;

function of_stop_clinic_dict(in_item_code in varchar2, out_message out varchar2) -- 实现有返回值的函数
return Integer is
backNumber INTEGER := 0;
begin
out_message := '返回值信息';
return backNumber ;
end of_stop_clinic_dict;
end test_pkg;



--调用程序包,这个仅测试用
set serveroutput on
declare
testSum number:=1;
testInteger integer:=1;
testPram varchar2(50):= '测试';
begin
test_pkg.showMessage;
testSum:=test_pkg.myAdd(10,11);
testInteger:=TEST_PKG.OF_STOP_CLINIC_DICT('123_45',testText1);
dbms_output.put_line(testSum);
dbms_output.put_line(testInteger);
end;

-- 调用只有一个入参一个返回值的程序包示例

SET SERVEROUTPUT ON;
DECLARE
result varchar2(100);
BEGIN
PKG_SPD_YEWU.PRC_SPD_GZHC_JKJKKQGB('0',result);
DBMS_OUTPUT.PUT_LINE('result: ' || result);
end;



posted @ 2021-09-16 16:47  孤舟蓑笠翁·  阅读(348)  评论(0编辑  收藏  举报