2024.07.12oracle函数练习

--1.创建一个函数,以员工号为参数,返回该员工的工资:
create or replace function f1 (
    eno in emp.empno%type  

return number
is
    vsal emp.sal%type; 
begin
    execute immediate 'SELECT sal FROM emp WHERE empno = :1' into vsal using eno;
    return vsal;
exception
    when no_data_found then
        return null;
    when others then
        dbms_output.put_line('Error: ' || sqlerrm);
        return null;
end;

declare
  v number;
begin
  v:=f1(7749);
  dbms_output.put_line(v);
  dbms_output.put_line(f1(7749));
end;
--2.创建一个函数,以部门号为参数,返回该部门的平均工资:
create or replace function f1 (
    deptno in emp.deptno%type  

return number
is
    dn number; 
begin
    execute immediate 'SELECT avg(sal) FROM emp WHERE deptno = :1' into dn using deptno;
    return dn;
exception
    when no_data_found then
        return null;
    when others then
        dbms_output.put_line('Error: ' || sqlerrm);
        return null;
end;


declare
  v number;
begin
  v:=f1(10);
  dbms_output.put_line(v);
end;
--3.创建一个函数,以员工号为参数,返回该员工所部门的平均工资:

create or replace function f1 (
    eno in emp.empno%type  

return number
is
    vsal number; 
begin
    execute immediate 'SELECT avg(sal) FROM emp WHERE deptno = (select deptno from emp where empno=:1)' into vsal using eno;
    return vsal;
exception
    when no_data_found then
        return null;
    when others then
        dbms_output.put_line('Error: ' || sqlerrm);
        return null;
end;


declare
  v number;
begin
  v:=round(f1(7749));
  dbms_output.put_line(v);
end;

--4.创建一个函数,求一个数的阶乘:
create or replace function f1 (n in number) 
return number
is
    temp number := 1;   
begin
    if n < 0 then
        return null;
    elsif n = 0 or n = 1 then
        return 1;
    else
        for i in 1..n loop
            temp := temp * i;
        end loop;
        return temp;
    end if;
exception
    when others then
        dbms_output.put_line('Error: ' || sqlerrm);
        return null;
end;

declare
  v number;
begin
  v:=round(f1(5));
  dbms_output.put_line(v);
end;

--5.创建函数,输入一个员工名字,判断该名字在员工表中是否存在。存在返回 1,不存在返回 0:
create or replace function f1 (en in varchar2) 
return number
is
    v_exists number;  
begin
    begin
        select case when exists (
            select 1
            from emp
            where ename = en
        ) then 1 else 0 end
        into v_exists
        from dual;
    exception
        when others then
            dbms_output.put_line('Error: ' || sqlerrm);
            return null;
    end;
   
    return v_exists;
end;


declare
  v VARCHAR2(20);
begin
  v:=f1('SMITH');
  dbms_output.put_line(v);
end;
--6.创建函数,给定员工编号,返回一句话:“该员工是:xxx,岗位是:xxx,薪资是:xx元。”若该编号对应的员工不存在,则返回:
--“所查工号不存在,请确认输入内容!”:
create or replace function f1 (eno in number) 
return varchar2
is
    en   varchar2(50);  
    jb   varchar2(50);
    sl   number;       
    result varchar2(200); 
begin
    begin
        execute immediate 'select ename, job, sal from emp where empno = :1' 
        into en, jb, sl 
        using eno;
        result := '该员工是:' || en || ',岗位是:' || jb || ',薪资是:' || sl || '元。';
    exception
        when no_data_found then
            result := '所查工号不存在,请确认输入内容!';
        when others then
            result := '错误: ' || sqlerrm;
    end;
    return result;
end;
   
declare
    v_result varchar2(200);
begin
    v_result := f1('7749');  
    dbms_output.put_line(v_result);

    v_result := f1('9999'); 
    dbms_output.put_line(v_result);
end;

--7.模拟length函数,编写一个lengthpro函数,在返回字符串的长度时,如果字符串里包含大写字母、小写字母和数字需要返回提示信息,
--输出“长度是xxx,里面包含大写字母|小写字母|数字(三者有几个显示几个)”:


--6.创建函数,给定员工编号,返回一句话:“该员工是:xxx,岗位是:xxx,薪资是:xx元。”
--若该编号对应的员工不存在,则返回:“所查工号不存在,请确认输入内容!”:
create or replace function f6(eno number)
return varchar2
is

  cnt number;
  str varchar2(500);
  ena emp.ename%type;
  ejob emp.job%type;
  esal emp.sal%type;
begin
  select count(*) into cnt from emp where empno = eno;
  if cnt > 0 then
    select ename,job,sal into ena,ejob,esal from emp where empno = eno;
    str := '该员工是:'||ena||',岗位是:'||ejob||',薪资是:'||esal||'元。';
  else
    str := '所查工号不存在,请确认输入内容!';
  end if;
  return str;
exception
  when others then
    dbms_output.put_line(sqlerrm); 
end;


select f6(7369) from dual;

--7.模拟length函数,编写一个lengthpro函数,在返回字符串的长度时,
--如果字符串里包含大写字母、小写字母和数字需要返回提示信息,输出“长度是xxx,
--里面包含大写字母|小写字母|数字(三者有几个显示几个)”:
create or replace function f1(str varchar2)
return varchar2
is
  res varchar2(500);
  n number := 1;
  low number := 0;
  up number := 0;
  numb number := 0;
  s varchar2(500) ;
begin
  loop
    exit when n>length(str);
    s := substr(str,n,1);
    if ascii(s) between 65 and 90 then
      up := up+1;
    elsif ascii(s) between 97 and 122 then
      low := low+1;
    elsif ascii(s) between 48 and 57 then
      numb := numb+1;
    end if;
    n := n+1;
  end loop;
  res := '长度是'||to_char(n-1)||',里面包含大写字母:'||up||'个,小写字母:'||low||'个,数字:'||numb||'个。';
  return res;
exception
  when others then
    dbms_output.put_line(sqlerrm); 
end;

declare
  s varchar2(500);
begin
  s:=f1('qwertyuiopabcABC123');
  dbms_output.put_line(s);
end;
 

  • 25
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Oracle函数大全.chm" 是一个包含了 Oracle 数据库中所有函数的电子文档。Oracle 是一种强大的关系型数据库管理系统,它提供了各种功能和特性,帮助用户有效地管理和操作数据库。 这本电子文档收录了 Oracle 数据库的各种函数,包括数值函数、字符串函数、日期和时间函数、系统函数等等。这些函数可以对数据库中的数据进行各种计算、转换、格式化和处理,帮助用户更好地控制和操作数据。 数值函数包括进行数学运算的函数,如加减乘除、取绝对值、取整数等等。字符串函数可以进行字符串的拼接、截取、替换、转换大小写等操作。日期和时间函数可以进行日期和时间的计算和转换,如取当前日期、计算日期差等。系统函数包括一些与数据库管理和性能优化相关的函数,如获取数据库版本、查询表结构等。 这本电子文档提供了各个函数的详细说明,包括函数的语法、参数说明和使用示例。对于 Oracle 数据库的开发人员和管理员来说,这本函数大全是一个非常有用的参考资料,可以帮助他们快速了解和掌握各种函数的用法和功能。 总之,"Oracle函数大全.chm" 是一本详尽的电子文档,它为用户提供了丰富的 Oracle 数据库函数的信息,帮助他们更好地利用这些函数进行数据库管理和开发工作。无论是初学者还是有经验的用户,都可以从这本电子文档中获得帮助,并加深对 Oracle 数据库函数的理解和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值