oracle 自定义函数(非常简单明了)

语法说明

复制代码
create [or replace] function functionName   (parameterName1 mode1 dataType1,  parameterName2 mode2 dataType2,  ...)  
 return returnDataType  
 is/as  
 	-- 定义使用变量、返回变量
 begin  
 	function_body  
 	return expression  
end functionName; -- 结束函数的声明,也可以直接写end不加函数名。  

-- 其中mode1、mode2表示参数类型,dataType表示参数的数据类型。returnDataType表示返回值类型。  

举例说明

1.举一个简单的例子

定义一个简单的函数,计算两数之和

复制代码
create or replace function useEasy(add1 in number, add2 in number) 
  return number 
  is
    FunctionResult number;
  begin
    FunctionResult := add1 + add2;
    return(FunctionResult);
end useEasy;

函数的使用请继续往下看

2.举一个复杂的例子(虽然复杂,但是很实用)

1⃣️、定义函数的返回类型

创建 TYPE 类型 atrr_type

复制代码
1、CREATE OR REPLACE TYPE atrr_type  AS OBJECT (
       attrId varchar2(40),
       objType varchar2(40)
);

2、将 TYPE 类型 atrr_type 定义为表, 用做接收返回值

复制代码
CREATE OR REPLACE TYPE attr_table AS TABLE of atrr_type;

2⃣️、定义函数(这里介绍三种方式)

1、以游标形式返回,有很大的局限性

复制代码
create or replace function selectAttrId(objType in VARCHAR2)
  return SYS_REFCURSOR 
  is
    attrId SYS_REFCURSOR;
  begin
    OPEN attrId FOR
      select attr_id, obj_type from CPS_OBJ_ATTR where obj_type = objType;
    return(attrId);
end selectAttrId;

2、以 Table 形式 返回结果集

复制代码
create or replace function resultFunction(objType in VARCHAR2)
  return attr_table 
  is
    attr_row 	atrr_type;        	 		  -- 定义单条数据变量
    attr     	attr_table := attr_table();   -- 定义返回结果,并初始化
  begin
    for thisrow in (select attr_id as attrId, obj_type as objType from CPS_OBJ_ATTR where obj_type = objType) 
    loop
      attr_row := atrr_type(thisrow.attrId, thisrow.objType);
      attr.extend;
      attr(attr.count) := attr_row;
    end loop;
  return(attr);
end resultFunction;

3、以管道形式返回结果集

复制代码
create or replace function returnPiperesult(objType in VARCHAR2)
  return attr_table pipelined
  is
    attr_row atrr_type;		 --定义attr_row为行对象类型
  begin
    for thisrow in (select attr_id as attrId, obj_type as objType from CPS_OBJ_ATTR where obj_type = objType)
    loop
      attr_row:= atrr_type(thisrow.attrId, thisrow.objType);
      pipe row (attr_row);
    end loop;
  return;
end returnPiperesult;

3⃣️、函数的使用

复制代码
select resultFunction('turck') from dual;			-- 直接调用函数

select * from table(resultFunction('turck'));  	-- 返回结果集table时,可以调用(自定义一个type为table 作为返回结果集)

觉得文章实用,请在右上方点个?

相关推荐
一只自律的鸡7 小时前
【MySQL】第二章 基本的SELECT语句
数据库·mysql
liliangcsdn8 小时前
如何使用python创建和维护sqlite3数据库
数据库·sqlite
TDengine (老段)14 小时前
TDengine 数学函数 DEGRESS 用户手册
大数据·数据库·sql·物联网·时序数据库·iot·tdengine
TDengine (老段)15 小时前
TDengine 数学函数 GREATEST 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
安当加密15 小时前
云原生时代的数据库字段加密:在微服务与 Kubernetes 中实现合规与敏捷的统一
数据库·微服务·云原生
爱喝白开水a15 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
想ai抽15 小时前
深入starrocks-多列联合统计一致性探查与策略(YY一下)
java·数据库·数据仓库
武子康15 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql
longgyy16 小时前
5 分钟用火山引擎 DeepSeek 调用大模型生成小红书文案
java·数据库·火山引擎
ytttr87316 小时前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#