oracle10gr2
学了下数据库存储过程,总结一下。对刚才学习有所交代。
存储过程结构:
create or replace procedure 过程名(参数1,参数2,参数3......)
as
数据定义(i number)
begin
存储过程体
end;
/
一、参数1,参数2,参数3
参数类型有:
IN :值传递,默认的
IN OUT :带值进,带值出
OUT:不带值进,带值出
参数1,可以这样定义:参数名 类型 数据类型。如:name IN OUT dept.dname%TYPE。
创建存储过程,判断如果deptno不存在,则插入一条记录。
create or replace procedure myproc(dno in out dept.deptno%type,name dept.dname%type,dl dept.loc%type)
as
cou number;
begin
select count(deptno) into cou from dept where deptno=dno;
if cou=0 then
insert into dept(deptno,dname,loc) values(dno,name,dl);
dno:=1;
else
dno:=-1;
end if;
end;
/
执行该过程。
declare
deptno dept.deptno%type;
begin
deptno:=12;
myproc(deptno,'haha','shenyang');
DBMS_OUTPUT.put_line(deptno);
end;
/
再试试参数类型为OUT类型的。
create or replace procedure myproc(dno OUT dept.deptno%type)
as
I number;
begin
I:=dno;
dno:=12;
end;
/
执行该过程:
declare
dno dept.deptno%type;
begin
dno:=12;
myproc(dno);
DBMS_OUTPUT.put_line(dno);
end;
/