剑轩

Oracle自定义函数的简单使用

电脑版发表于:2018/12/28 15:13

一.最最最简单的返回一个数字的函数

create or replace function fun_show
return int --申明返回值
as
begin
       return 1;
end;

函数执行可以配合sql语句:

 select fun_show() from dual


二、根据用户名查询工资的函数

create or replace function func_select(pename nvarchar2)
return int --申明返回值
as
 psal int;--定义临时变量接收查询的返回结果
begin
       select sal into psal from scott.emp where ename = pename;
       return psal;
end;

可以直接在begin,end代码块执行输出:

begin
  dbms_output.put_line(func_select('CLARK'));
end;

也可以使用一个变量接收后再输出:

declare psal int;--申明一个变量
begin
  psal := func_select('CLARK'); --注意oracle中赋值是使用:=
  dbms_output.put_line(psal);
end;



未完待续^.^

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢