`

存储过程总结

阅读更多

完整的过程结构如下:
create or replace procedure
过程名 as

声明语句段;
begin
执行语句段
;
exception
异常处理语句段
;
end;

过程是有名称的程序块,as关键词代替了无名块的declare

 

以下是根据例子说明各种存储过程的调用:

create table student(

       sno number(6) primary key,

       sname varchar2(25)

       constraint s_sname_nn not null,

       ssex varchar2(6),

       sage number(6),

       sdept varchar2(25)

)

create sequence stu_seq;

insert into student values(stu_seq.nextval,'dengwenjun','male',24,'Information');

select * from student

 

创建存储过程

--1.无参数存储过程

create or replace procedure stu_pro

as

p_name student.sname%type;

--p_name是申明的一个变量,它的类型是与表studentsname字段类型一致

begin

  select sname into p_name from student where sno =28;

  dbms_output.put_line(p_name);

  end;

-- 不能忘记from table(student)

call stu_pro();

--无论是有参数还是无参数,调用时"()"是不可少的

 

--2.仅有输入参数的过程

create or replace procedure stu_pro1(pno in student.sno%type)

as

p_name student.sname%type;

begin

  select sname into p_name from student where sno =pno;

  dbms_output.put_line(p_name);

  end;

  --in表示pno为输入参数,pno是参数名称

  call stu_pro1(30);

  --pno30sql语句的意思是把表中sno=30sname赋给p_name

 

--3.仅有输出参数的存储过程

create or replace procedure stu_pro2(pname out student.sname%type)

as

begin

  select sname into pname from student where sno = 31;

  dbms_output.put_line(pname);

  end;

  --这里有一个输出参数,as后不用再申明了

  --这里调用存储过程时不能直接用call来调用,call stu_pro2(?)是错误的,针对有输入参数的。这种情况的调用将在下面oracle函数调用中说明

 

--4.同时有输入/输出参数的存储过程

create or replace procedure stu_pro3

(pno in student.sno%type,pname out student.sname%type)

as

begin

  select sname into pname from student where sno=pno;

  dbms_output.put_line(pname);

  end;

  --这里的存储过程也不能直接用call来调用,这种情况的调用将在下面oracle函数调用中说明

 

Oracle函数调用存储过程

--1.对无参数过程的调用

  --函数

create or replace function get_pname return varchar2

--is相当于过程中的as,申明返回类型return varchar2

is

pname varchar2(25);

begin

  stu_pro;

  --调用存储过程stu_pro

  select sname into pname from student where sno=32;

  return pname;

  --返回结果

  end;

--调用函数

declare

begin

  dbms_output.put_line('PL/SQL中打印的结果:'||get_pname);

end;

 

--2.对有输入参数过程的调用

create or replace function get_pname1(pno in student.sno%type) return varchar2

is

pname varchar2(25);

begin

  stu_pro1(pno);

  --调用有参数的存储过程,直接写入输入参数名

  select sname into pname from student where sno=pno;

  return pname;

  end;

  --调用函数

  declare

  begin

    dbms_output.put_line('PL/SQL中打印的结果:'||get_pname1(25));

    end;

 

--3.对有输出参数过程的调用

create or replace function get_pname2(pname out varchar2) return varchar2

is

begin

  stu_pro2(pname);

  return pname;

  end;

  --调用函数

  declare

  p_name student.sname%type;

  begin

    dbms_output.put_line('PL/SQL中打印的结果:'||get_pname2(p_name));

    end;

 

--4.对有输入/输出参数过程的调用

create or replace function get_pname3(pno in number,pname out varchar2)return varchar2

----这里pno,pname的类型不能选定范围

is

begin

  stu_pro3(pno,pname);

  return pname;

  end;

  --调用函数

  declare

  pname varchar2(25);

  begin

    dbms_output.put_line('结果是:'||get_pname3(25,pname));

  end;

  --注意最后的end;

---------------------------------------------------------------------

    对于有输出参数的存储过程,需要使用函数调用,函数调用返回对应的输出参数的值,再通过调用函数获得最后的值。

    注意创建函数时必须有return。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics