北大青鸟oracle学习笔记23 24

子程序

命名的pl/sql块
模块化、可重用性、可维护性

过程
create procedure 过程名 [参数1 in|out|in out…]
is|as
    局部变量声明
begin
    执行语句
end;

参数模式:
in
    接受值 默认模式 (接受输入参数)
out
    将值返回给子程序的调用 (将过程中改变后的值带出过程,类似于函数返回值)不能带精度 如varchar(10),只能为varchar
in out
    接受值并返回已更新的值 (接受输入参数并将过程中改变后的值带出过程)

执行过程:
    过程名(参数里表);

create or replace
procedure add123 as
  i integer;
  j integer;
begin
  i:=1;
  j:=2;
  dbms_output.put_line(i+j);
end;
execute add123;
注意这里创建过程和执行不能放在一起,否在execute会随过程定义写入过程中,sql develop中测试如此,不知道为什么。
CREATE OR REPLACE
PROCEDURE QUERYSTUNAME
( stuid STUDENT.STU_ID%TYPE) 
AS
  sname student.stu_name%type;
  ssex student.stu_sex%type;
BEGIN
  select stu_name,stu_sex into sname,ssex from student where stu_id = stuid;
  dbms_output.put_line(sname||ssex);
EXCEPTION
  when No_Data_Found then
    dbms_output.put_line('no data found');
  when Too_Many_Rows then
    dbms_output.put_line('to many rows');
  when others then
    dbms_output.put_line('other error');
END QUERYSTUNAME;

execute querystuname(1);
 
函数

create function 函数名 [参数列表]
return 数据类型 is|as
    局部变量声明
begin
    可执行语句
end;

两种访问方式
pl/sql块
sql语句

仅接受in参数,返回值类型不能带精度。

CREATE OR REPLACE
FUNCTION GETNAME(stuid varchar)
RETURN VARCHAR AS
  stuname student.stu_name%type;
BEGIN
  select stu_name into stuname from student where stu_id = stuid;
  RETURN stuname;
EXCEPTION
  when No_Data_Found then
    dbms_output.put_line('no data found');
  when Too_Many_Rows then
    dbms_output.put_line('to many rows');
  when others then
    dbms_output.put_line('other error');
END GETNAME;

declare
begin
  dbms_output.put_line(getname(1));
end;

过程函数
作为pl/sql语句执行作为表达式一部分调用
在规格说明中不包含return子句必须在规格说明中包含return子句
可以返回任何值返回单值
可以包含return语句,但是不能用于返回值必须包含return语句
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值