zoukankan      html  css  js  c++  java
  • oracle 之 函数

    本次主题 青涩/色

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

    --语法:

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

     return数据类型

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

     [局部变量声明]

     begin

     pl/sql函数体

     end[<函数名>]

    --函数 没有参数

    create or replace function getCount
    return number
    as  v_num number;

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

    --调用函数1
    select 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 varchar2
    as
    v_person v_emp%rowtype;
    v_str varchar2(100);
    begin
    select * 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 varchar2
    as
    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 varchar2
    as
    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;

  • 相关阅读:
    Educational Codeforces Round 10 C. Foe Pairs 水题
    Educational Codeforces Round 10 B. z-sort 构造
    CDOJ 1048 Bob's vector 三分
    Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
    第14届电子科大初赛民间盗版部分题目题解
    HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
    HDU 5653 Bomber Man wants to bomb an Array. dp
    HDU 5652 India and China Origins 二分+并查集
    HDU 5651 xiaoxin juju needs help 数学
    HDU 5650 so easy 数学
  • 原文地址:https://www.cnblogs.com/cmyxn/p/5877273.html
Copyright © 2011-2022 走看看