orcale存储过程学习之路(一)

 -----------------------------------------  建表  ------------------------------------------

create table test_user
(
id VARCHAR2(12),
name VARCHAR2(32),
age number(2)
)
select t.id,t.name,t.age from test_user t


insert into test_user (ID, NAME,age)
values ('1', '张三',12);

insert into test_user (ID, NAME,age)
values ('2', '李四',12);

insert into test_user (ID, NAME,age)
values ('3', '王五',12);


insert into test_user (ID, NAME,age)
values ('4', '赵六',21);

insert into test_user (ID, NAME,age)
values ('5', '田七',21);

 -----------------------------------------  存储过程  ------------------------------------------

1.在packages文件夹下新建一个“包”,当新建时会同时出现两个供编辑的面板:一个为“声明”的,一个为“body”部分的。

2.编写两个空方法:

-----------------------------------------  声明部分  ------------------------------------------

create or replace package Pro_Test_User is
PROCEDURE Ins_Test_User(I_ID IN VARCHAR2,I_NAME IN VARCHAR2,I_AGE IN VARCHAR2,O_RET_CODE OUT NUMBER,O_RET_MSG OUT VARCHAR2);
PROCEDURE Upd_Test_User(I_ID IN VARCHAR2,I_NAME IN VARCHAR2,I_AGE IN VARCHAR2,O_RET_CODE OUT NUMBER,O_RET_MSG OUT VARCHAR2);
PROCEDURE Del_Test_User(I_ID IN VARCHAR2,O_RET_CODE OUT NUMBER,O_RET_MSG OUT VARCHAR2);
PROCEDURE Que_Test_User(I_ID IN VARCHAR2,I_NAME IN VARCHAR2,I_AGE IN VARCHAR2,O_RET_CODE OUT NUMBER,O_RET_MSG OUT VARCHAR2,O_RET_LIST OUT SYS_REFCURSOR);
PROCEDURE Que_Page_Test_User(I_ID IN VARCHAR2,I_NAME IN VARCHAR2,I_AGE IN VARCHAR2,I_PAGE_SIZE IN NUMBER,I_PAGE_NUM IN NUMBER,O_RET_CODE OUT NUMBER,O_RET_MSG OUT VARCHAR2,O_RET_TOTNUM OUT NUMBER,O_RET_LIST OUT SYS_REFCURSOR);
end Pro_Test_User;

-----------------------------------------  BODY部分  ------------------------------------------

create or replace package body Pro_Test_User is
PROCEDURE Ins_Test_User(I_ID IN VARCHAR2,
I_NAME IN VARCHAR2,
I_AGE IN VARCHAR2,
O_RET_CODE OUT NUMBER,
O_RET_MSG OUT VARCHAR2)IS
BEGIN
O_RET_CODE:=0;
O_RET_MSG:='初始化默认成功';
IF I_ID IS NOT NULL AND I_NAME IS NOT NULL AND I_AGE IS NOT NULL THEN
INSERT INTO Test_User(ID,NAME,AGE) VALUES (I_ID,I_NAME,I_AGE);
COMMIT;
O_RET_CODE:=0;
O_RET_MSG:='操作成功';
ELSE
O_RET_CODE:=1;
O_RET_MSG:='没有ID|姓名|年龄';
END IF;
EXCEPTION
WHEN OTHERS THEN
O_RET_CODE:=-1;
O_RET_MSG:='sql执行过程出错啦!';
END Ins_Test_User;

PROCEDURE Upd_Test_User(I_ID IN VARCHAR2,
I_NAME IN VARCHAR2,
I_AGE IN VARCHAR2,
O_RET_CODE OUT NUMBER,
O_RET_MSG OUT VARCHAR2)IS
BEGIN
O_RET_CODE:=0;
O_RET_MSG:='初始化默认成功';
IF I_ID IS NOT NULL THEN
UPDATE Test_User SET NAME=I_NAME,
AGE =I_AGE
WHERE ID= I_ID;
COMMIT;
O_RET_CODE:=0;
O_RET_MSG:='操作成功';
ELSE
O_RET_CODE:=1;
O_RET_MSG:='没有ID';
END IF;
EXCEPTION
WHEN OTHERS THEN
O_RET_CODE:=-1;
O_RET_MSG:='sql执行过程出错啦!';
END Upd_Test_User;

PROCEDURE Del_Test_User(I_ID IN VARCHAR2,
O_RET_CODE OUT NUMBER,
O_RET_MSG OUT VARCHAR2)IS
BEGIN
NULL;
END Del_Test_User;

PROCEDURE Que_Test_User(I_ID IN VARCHAR2,
I_NAME IN VARCHAR2,
I_AGE IN VARCHAR2,
O_RET_CODE OUT NUMBER,
O_RET_MSG OUT VARCHAR2,
O_RET_LIST OUT SYS_REFCURSOR)IS
T_SQL VARCHAR2(200):='';
U Test_User%rowtype;
BEGIN
NULL;
IF I_NAME IS NOT NULL THEN
T_SQL := T_SQL || 'AND T.NAME = ''' || I_NAME || '''';
END IF;
IF I_AGE IS NOT NULL THEN
T_SQL := I_AGE || 'AND T.AGE = ''' || I_AGE || '''';
END IF;
T_SQL := 'SELECT T.ID,T.NAME,T.AGE FROM Test_User T WHERE 1=1'|| T_SQL ;
OPEN O_RET_LIST FOR T_SQL;
loop
fetch O_RET_LIST into U.id, U.name,U.age;
exit when O_RET_LIST%notfound;
dbms_output.put_line(U.id||'-'||U.name||'-'||U.age);
end loop;
END Que_Test_User;

PROCEDURE Que_Page_Test_User(I_ID IN VARCHAR2,
I_NAME IN VARCHAR2,
I_AGE IN VARCHAR2,
I_PAGE_SIZE IN NUMBER,
I_PAGE_NUM IN NUMBER,
O_RET_CODE OUT NUMBER,
O_RET_MSG OUT VARCHAR2,
O_RET_TOTNUM OUT NUMBER,
O_RET_LIST OUT SYS_REFCURSOR)IS
O_Q_SQL VARCHAR2(200):='';
O_T_SQL VARCHAR2(200):='';
I_PRA VARCHAR2(200):='';
BEGIN_ROW NUMBER :=0;
END_ROW NUMBER :=I_PAGE_SIZE;
BEGIN
O_RET_CODE:=0;
O_RET_MSG:='初始化默认成功';
IF I_PAGE_SIZE IS NOT NULL AND I_PAGE_NUM IS NOT NULL THEN
BEGIN_ROW := (I_PAGE_NUM-1)*I_PAGE_SIZE+1;
END_ROW := I_PAGE_NUM *I_PAGE_SIZE ;
IF I_NAME IS NOT NULL THEN
I_PRA := I_PRA || 'AND T.NAME LIKE ''%' || I_NAME || '%''';
END IF;
IF I_AGE IS NOT NULL THEN
I_PRA := I_PRA || 'AND T.AGE = ''' || I_AGE || '''';
END IF;
O_Q_SQL := 'SELECT T.ID,T.NAME,T.AGE FROM (SELECT ROWNUM RN ,TE.* FROM Test_User TE WHERE ROWNUM <='|| END_ROW ||') T WHERE T.RN >= '|| BEGIN_ROW || I_PRA ;
OPEN O_RET_LIST FOR O_Q_SQL;
O_T_SQL := 'SELECT COUNT(ID) AS TOTNUM FROM Test_User WHERE 1=1'|| I_PRA ;
DBMS_OUTPUT.put_line(O_T_SQL);
O_RET_CODE:=0;
O_RET_MSG:='操作成功';
ELSE
O_RET_CODE:=1;
O_RET_MSG:='没有页码详情';
END IF;
DBMS_OUTPUT.put_line(O_T_SQL);
DBMS_OUTPUT.put_line(O_RET_MSG);
EXCEPTION
WHEN OTHERS THEN
O_RET_CODE:=-1;
O_RET_MSG:='sql执行过程出错啦!';
END Que_Page_Test_User;

end Pro_Test_User;

3.存储语句写完后在执行一个左上角的“设置”按钮。

4.查看packages和package bodies下是否有生成的存储过程

 

 

调用(一):

declare
I_ID VARCHAR2(10);
I_NAME VARCHAR2(10);
I_AGE VARCHAR2(10);
O_RET_CODE NUMBER;
O_RET_MSG VARCHAR2(10);
p_ref1 SYS_REFCURSOR;
result_ SYS_REFCURSOR;
begin
Pro_Test_User.Que_Test_User(I_ID,I_NAME,I_AGE,O_RET_CODE,O_RET_MSG,p_ref1);
DBMS_OUTPUT.put_line(O_RET_MSG);
end;

 调用(二):

declare
I_ID VARCHAR2(10);
I_NAME VARCHAR2(10);
I_AGE VARCHAR2(10);
O_RET_CODE NUMBER;
O_RET_MSG VARCHAR2(10);
p_ref1 SYS_REFCURSOR;
result_ SYS_REFCURSOR;
begin
Pro_Test_User.Que_Test_User(I_ID,I_NAME,I_AGE,O_RET_CODE,O_RET_MSG,O_RET_LIST=>p_ref1);
loop
fetch p_ref1 into I_ID,I_NAME,I_AGE;
dbms_output.put_line(I_NAME||'------------'||I_AGE);
exit when p_ref1%notfound;
end loop;
end;

 

调用(三):

declare
v_cursor sys_refcursor;
ret_code NUMBER;
ret_msg VARCHAR2(10);
u Test_User%rowtype;
begin
Pro_Test_User.Que_Test_User(2,'张三','',ret_code,ret_msg,v_cursor);
loop
fetch v_cursor into u.id, u.name,u.age;
exit when v_cursor%notfound;
dbms_output.put_line(u.id||'-'||u.name||'-'||u.age);
end loop;
end;

posted @ 2018-06-25 11:42  baby孔祥超  阅读(307)  评论(0编辑  收藏  举报