搜索
您的当前位置:首页正文

oracle 之 函数

2023-11-10 来源:哗拓教育

函数的结束一定要使用return语句返回一个与声明匹配的值

--语法:

 create[or replace] function<函数名> [(参数列表)]

 return数据类型

 is|as (is或as完全等价 )

 [局部变量声明]

 begin

 pl/sql函数体

 end[<函数名>]

 

--函数 没有参数

create or replace function getCountreturn numberas  v_num number;

begin   select count(*) into v_num from v_emp;   return v_num;end;

--调用函数1select getCount() from dual;--调用函数2 plsql语句块declare num number;begin num := getCount(); dbms_output.put_line(num);end;

--带有 in 参数的函数, in 默认 ,可以使用select语句和plsql语句块调用函数

create or replace function getName(v_name varchar2)return varchar2asv_person v_emp%rowtype;v_str varchar2(100);beginselect * into v_person from v_emp where ename = v_name;v_str := ‘当前人是‘||v_person.ename||‘ 工资是‘||v_person.sal;return v_str;end;

--调用函数  select语句

select getPersonByName(‘SMITH‘) from dual;

 --调用函数  plsql语句块

declare a_name varchar2(50);begin a_name := getName(‘SMITH‘); dbms_output.put_line(a_name);end;

 --带有 out 参数的函数   函数携有out参数的,只能使用plsql语句块调用函数

create or replace function getSal(p_name varchar2,e_sal out number)return varchar2as v_st varchar2(100); begin select sal into e_sal from v_emp where ename=p_name; v_st := p_name||‘每个月开‘||e_sal||‘元‘; return v_st; end;

--调用函数 plsql语句块

declare v_str varchar2(20); v_sal number; begin v_str := getSal(‘SMITH‘,v_sal); dbms_output.put_line(v_str); dbms_output.put_line(v_sal); end;

-- 带有 in out 参数的函数 同样有out参数的函数,只能由plsql语句块调用函数

create or replace function swap(num1 in out number,num2 in out number)return varchar2as temp number;begin temp := num1; num1 := num2; num2 := temp; return ‘abc‘;end;

 --调用函数

declare num1 number := 10; num2 number := 20; v_str varchar2(20);begin dbms_output.put_line(num1||‘==========‘||num2); v_str := swap(num1,num2); dbms_output.put_line(num1||‘==========‘||num2);end;

oracle 之 函数

标签:

小编还为您整理了以下内容,可能对您也有帮助:

oracle 如何取表中所有数据的20%

select t.*,t.rid from (select * ,rownum as rid from table) t

where rid<= (select round(count(*)*0.2) as count from table);

怎么使用oracle语句查找一张表中在平均值上下20%范围内的数据拿出来?

一条语句太长了,写两条吧!

select avg([字段]) from [表名]

先求出平均值,然后算出来*1.2,*0.8

第二条语句select * from [表名] where ([字段]<1.2*平均值 and [字段]>0.8*平均值)

oracle 数据库中 某一组数据在原来的基础增加百分之二十 该怎么写

UPDATE 表名 SET 字段名 = 字段名 * 1.2 WHERE 条件(假设有)

Top