Oracle中的函数与存储过程
自定义函数
函数的最大特征是必须返回值
创建自定义函数
创建函数使用create or replace function命令
create or replace function 函数名 return 返回值类型 as
begin
函数定义
end 函数名;
create or replace function getEmployeeCount return number as
begin
declare employee_count number;
begin
select count(1) into employee_count from employees;
return employee_count;
end;
end getEmployeeCount;
调用自定义函数
在pl/sql的环境中利用 函数名+括号 的形式直接调用函数
函数的参数
create or replace function 函数名 (数据类型1 参数1,数据类型2 参数2...)return 返回值类型 as
begin
函数定义
end 函数名;
create or replace function getTableCount(table_name varchar2) return number as
begin
declare record_count number;
aquery_sql varchar(200);
begin
query_sql := 'select count(1) from ' || table_name;
execute immediate_sql query_sql into record_count;
return record_count;
end;
end getTableCount;
execute immediate_sql query_sql into record_count;用于执行动态sql语句query_sql,并将结果置于变量record_count中
利用函数的确定性提高数据库效率
对于一个函数来说,其执行步骤和代码是固定不变的。有时,对于一个函数来说,只要传入的参数一定,那么返回值将不会发生任何改变,这样的函数称为具有确定性的函数。
对于函数,尤其是运算和操作步骤较为复杂的函数,每次为其传入参数,并最终返回运算结果是较为费时的。而函数的确定性,在于可以根据函数名和参数来缓存执行结果。当再次利用相同的参数调用函数时,Oracle将直接获得缓存值,而不会执行函数的实际代码,从而提高工作效率。
create or replace function getEaterAmout(ton number,unitPrice number) return number deterministic as
begin
declaer waterAmount number;
begin
if(ton<=2)then
waterAmount := unitPrice * ton;
end if;
if(ton>2 and ton<=4) then
waterAmount := unitPrice*2 +unitPrice*2*(ton-2);
end if;
if(ton>4) then
waterAmount := unitPrice*2 +unitPrice*2*2+(ton-4)*4*unitPrice;
end if;
return waterAmount;
end;
end getEaterAmout;
在return number之后添加deterministic关键字,表名所创建的函数具有确定性。
典型函数–行转列
create or replace function row2column(sqlString varchar2) return varchar2 as
begin
declare type cu_type is ref cursor;
tmp_cursor cu_type;
tmp_row varchar2(20);
v_result varchar2(500);
begin
open tmp_cursor for sqlString;
fetch tmp_cursor into tmp_row;
while tmp_cursor%found loop
v_result := v_result || tmp_row || ',';
fetch tmp_cursor into tmp_row;
end loop;
return rtrim(v_result,',');
end;
end row2column;
tmp_cursor cu_type;声明一个动态游标;open tmp_cursor for sqlString;在打开游标时,将传入的参数sqlString作为游标定义;while tmp_cursor%found loop则对游标tmp_cursor进行循环操作,将获得的值串联到变量v_result上
select row2column('select employee_name from employees where employee_age>30') result from dual;
获得表employee中,年龄大于30岁的员工姓名
存储过程
函数倾向于处理数据运算;返回值
存储过程倾向于数据库操作,并不返回值
创建存储过程
create or replace procedure 存储过程名称 as
begin
存储过程定义
end 存储过程名称
create or replace procedure updateStatus as
begin
update students set status='Act';
commit;
end updateStatus;
执行存储过程
直接利用存储过程名称调用和执行存储过程
begin
updateStatus;
end;
如果在命令行中,使用execute命令执行存储过程
execute updateStatus;
只进不出的参数–in参数
in参数可以在存储过程内部被访问,但不能被修改
create or replace procedure insertStudent(p_name in varchar2,p_age in number) as
begin
...
end insertStudent;
只出不进的参数–out参数
create or replace peocedure insertStudent(p_name in varchar2,p_age in carchar2,original_count out number,current_count out number) as
begin
declare v_max_id number;
begin
if(p_name is null or legnth(p_name)=0) then
return;
end if;
if(p_age<10 or p_age>30) then
return;
end if;
select count(1) into original_count from students;
select max(student_id) into v_max_id from students;
insert into students(student_id,student_name,student_age,status) values (v_max_id+1,p_name,p_age,'act');
select count(1) into current_count from students;
end;
end insertStudent;
调用
declare v_countk1 number;
v_count2 number;
begin
insertStudent('王云',17,v_countk1,v_count2);
dbms_output.put_line('原数量' || v_countk1);
dbms_output.put_line('现数量' || v_count2);
end;
可进可出的参数–in out 参数
程序包
程序包的规范
创建规范
CREATE OR REPLACE PACKAGE PKG_COVER_TEMPLATE_BASE AS
PROCEDURE P_UPDATE_PROJ_COVER_TEMPLATE;
function getStudentsName return varchar2;
end PKG_COVER_TEMPLATE_BASE;
程序包的主体
CREATE OR REPLACE PACKAGE BODY PKG_COVER_TEMPLATE_BASE AS
function getStudentsName return varchar2 is..(具体内容)..;
PROCEDURE P_UPDATE_PROJ_COVER_TEMPLATE() as..(具体内容)..
end PKG_COVER_TEMPLATE_BASE;
调用程序包中的函数/存储过程
select PKG_COVER_TEMPLATE_BASE.P_UPDATE_PROJ_COVER_TEMPLATE from dual;
Oracle中的函数与存储过程的更多相关文章
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- Oracle中wm_concat()函数的使用
Oracle中wm_concat()函数的使用 wm_concat()函数是oracle行列转换函数,该函数可以把列值以‘,’分割开来,并显示成一行. 1.原数据: 2.把结果分组以‘|’分隔,以一行 ...
- Oracle中trunc函数、round 函数、ceil函数和floor函数的使用
Oracle中trunc函数.round 函数.ceil函数和floor函数的使用 1.1trunc函数处理数字 trunc函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定 ...
- Oracle中REGEXP_SUBSTR函数(转)
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下:在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','2 ...
- Oracle中sign函数和decode函数的使用
Oracle中sign函数和decode函数的使用 1.比较大小函数SIGN sign(x)或者Sign(x)叫做 符号函数,其功能是取某个数的符号(正或负): 当x>0,sign(x)=1; ...
- oracle中extract()函数----用于截取年、月、日、时、分、秒
oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分 语法如下: extract ( { year | month | day ...
- oracle中使用函数控制过程是否执行(结合job使用)
oracle中使用函数控制过程是否执行(结合job使用时候,循环时间不好写的时候,可以此种方法比较方便) CREATE OR REPLACE FUNCTION wsbs_pk_date_validat ...
- Oracle中SYS_CONNECT_BY_PATH函数的妙用 ;
Oracle 中SYS_CONNECT_BY_PATH函数是非常重要的函数,下面就为您介绍一个使用SYS_CONNECT_BY_PATH函数的例子,实例如下: 数据准备: ),b )); ', 'A' ...
- oracle中floor函数和to_number函数区别
oracle中floor函数没有值默认是0,number函数没有值默认是空
- Oracle中REGEXP_SUBSTR函数(字符串转多行)
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20',' ...
随机推荐
- 《机器人SLAM导航核心技术与实战》第1季:第8章_激光SLAM系统
<机器人SLAM导航核心技术与实战>第1季:第8章_激光SLAM系统 视频讲解 [第1季]8.第8章_激光SLAM系统-视频讲解 [第1季]8.1.第8章_激光SLAM系统_Gmappin ...
- springboot接口接收xml
对xml文件的操作也可以借助hutool的XmlUtil. 1. xml格式 <root> <deviceStatInfo onlineCount="64" of ...
- 基于注解创建bean对象和注入方式
一.配置spring核心配置文件ApplicationContext.xml,添加扫描包 二.注解创建bean对象 三.注入方法
- Linux四剑客grep、find、sed、awk使用
介绍 Linux四剑客是指在Linux系统中非常常用的四个命令工具,它们分别是grep.find.sed和awk.这四个工具在Linux系统中具有非常强大的功能,可以方便快捷地对文本进行搜索.处理 ...
- 关于:js怎么获取元素的自定义属性的问题(原生JavaScript)
最近项目需要把后端传过来的数据隐藏的保存在页面中,方便后边做事件处理时使用.鉴于之前总是在后端处理后的页面中看到元素里除了常见的id.name属性外的data-xxx,就想到:元素的属性必然是可以自定 ...
- 备份一个迭代查找TreeViewItem的辅助函数
private TreeViewItem FindTreeItem(TreeViewItem item, Func<TreeViewItem, bool> compare) { if (i ...
- SpringBoot——使用http2
使用http2 许多浏览器,包括Edge,仅在TLS(即HTTPS)情况下支持HTTP/2.即使服务器端配置为无TLS支持的HTTP/2,浏览器可能仍将回退到HTTP/1.1.所以我们需要有一个证书来 ...
- dbeaver导入sql脚本报错的排查—— ERROR 1366 (HY000) at line
描述 在使用dbeaver进行sql脚本导入的时候报了以下的错误. C:\Users\xxxx\AppData\Roaming\DBeaverData\drivers\clients\mysql_8\ ...
- 代码随想录第七天 | Leecode 454.四数相加II 、383. 赎金信 、15. 三数之和 、18. 四数之和
Leecode 454. 四数相加II 题目链接:https://leetcode.cn/problems/4sum-ii/ 题目描述 给你四个整数数组 nums1.nums2.nums3 和 num ...
- GStreamer开发笔记(三):测试gstreamer/v4l2+sdl2/v4l2+QtOpengl打摄像头延迟和内存
前言 前面测试了多种技术路线,本篇补全剩下的2种主流技术,v4l2+sdl2(偏底层),v4l2+QtOpengl(应用),v4l2+ffmpeg+QtQImage(Image的方式转图低于1ms ...