子程序
命名的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;
| 过程 | 函数 |
| 作为pl/sql语句执行 | 作为表达式一部分调用 |
| 在规格说明中不包含return子句 | 必须在规格说明中包含return子句 |
| 可以返回任何值 | 返回单值 |
| 可以包含return语句,但是不能用于返回值 | 必须包含return语句 |
本文详细介绍了PL/SQL中的过程和函数的定义及使用方法,包括过程和函数的创建、参数传递方式以及如何调用等内容,并对比了两者之间的区别。
158

被折叠的 条评论
为什么被折叠?



