• oracle函数


    oracle函数创建及调用

    CREATE [OR REPLACE] FUNCTION function_name
    [ (argment [ { IN | OUT | IN OUT } ] Type ,
    argment [ { IN | OUT | IN OUT } ] Type ]
    RETURN return_type 
    { IS | AS }
    <类型.变量的说明> 
    BEGIN
    FUNCTION_body
    EXCEPTION
    其它语句
    END;

    示例:

    1) 在scott.emp表上创建一个函数deptsal,输入参数为员工编号,返回该员工所在部门的平均工资。

    (1)创建函数

    create or replace function deptsal(f_deptno in number) return number

    as

    avgsal number;

    begin

    select avg(sal) into avgsal from emp where deptno=f_deptno;

    return avgsal;

    end;

    /

    (2)调用函数

    declare

    avgsal number(10,2);

    begin

    avgsal:=deptsal(10);

    DBMS_OUTPUT.PUT_LINE('平均工资:'||avgsal);

    end;

    /

    (3)效果图

    2) 在scott.emp表上创建一个函数deptnum,输入部门编号,返回该部门的员工人数,并调用该函数。

    (1)创建函数

    create or replace function deptnum(f_deptno in number) return int

    as

    dept_count int;

    begin

    select count(1) into dept_count from emp where deptno=f_deptno;

    return dept_count;

    end;

    /

    (2)调用函数

    declare

    dept_count int;

    begin

    dept_count:=deptnum(10);

    DBMS_OUTPUT.PUT_LINE('部门人数:'||dept_count);

    end;

    /

    (3)效果图

  • 相关阅读:
    排序算法:冒泡排序
    排序算法: 插入排序
    排序算法:折半插入排序
    排序算法:快速排序
    浅谈C/C++回调函数(Callback)& 函数指针
    仿函数
    回溯法:求和
    动态规划:数字和为sum的方法数
    字典序
    剑指offer15 二进制中1的个数
  • 原文地址:https://www.cnblogs.com/remote/p/9992729.html
Copyright © 2020-2023  润新知