博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建存储过程和函数【weber出品必属精品】
阅读量:6874 次
发布时间:2019-06-26

本文共 2753 字,大约阅读时间需要 9 分钟。

一、什么是存储过程和函数

1. 是被命名的pl/sql块

2. 被称之为pl/sql子程序

3. 与匿名块类似,有块结构:

声明部分是可选的(没有declare关键字)

必须有执行部分

可选的异常处理部分

二、匿名块和子程序之间的区别

三、存储过程:语法

CREATE [OR REPLACE] PROCEDURE procedure_name[(argument1 [mode1] datatype1,argument2 [mode2] datatype2,. . .)]IS|ASprocedure_body;
create or replace procedure add_dept isv_deptment_id dept.deptno%type;v_deptment_name dept.dname%type;begin  v_deptment_id :=60;  v_deptment_name := 'YWB';  insert into dept(deptno,dname) values(v_deptment_id,v_deptment_name);  commit;  dbms_output.put_line('插入了:'||sql%rowcount||'行');  end;

使用匿名块调用存储过程:

begin  add_dept;end;

四、函数:

CREATE [OR REPLACE] FUNCTION function_name [(argument1 [mode1] datatype1,  argument2 [mode2] datatype2,  . . .)]RETURN datatypeIS|ASfunction_body;

函数与存储过程的区别:函数必须返回数据,存储过程可以返回数据,也可以不返回数据

create or replace function check_sal return boolean is  dept_id emp.deptno%type :=10;  emp_no emp.empno%type :=7788;  salary emp.sal%type;  avg_sal emp.sal%type;beginselect sal into salary from emp where empno=emp_no; select avg(sal) into avg_sal from emp where deptno=dept_id; if salary>avg_sal then   return true;   else     return false;     end if;   exception      when no_data_found then     return null;  end;

在匿名块中调用函数:

begin  if( check_sal is null) then  dbms_output.put_line('由于程序异常,输出null');  elsif (check_sal) then     dbms_output.put_line('工资高于平均工资');    else       dbms_output.put_line('工资低于平均工资');       end if;  end;

给函数传递参数:

create or replace function check_sal(empno number) return boolean is  dept_id employees.department_id%type;  sal     employees.salary%type;  avg_sal employees.salary%type;begin  select salary, department_id    into sal, dept_id    from employees   where employee_id = empno;  select avg(salary)    into avg_sal    from employees   where department_id = dept_id;  if sal > avg_sal then    return true;  else    return false;  end if;exception  when no_data_found then    return null;end;create or replace function check_sal(empno number) return number is  dept_id employees.department_id%type;  sal     employees.salary%type;  avg_sal employees.salary%type;begin  select salary, department_id    into sal, dept_id    from employees   where employee_id = empno;  select avg(salary)    into avg_sal    from employees   where department_id = dept_id;  if sal > avg_sal then    return 1;  elsif (sal = avg_sal) then    return 2;  else    return 3;  end if;exception  when no_data_found then    return null;end;begin  if (check_sal(200) is null) then    dbms_output.put_line('由于程序异常,输出NULL');  elsif (check_sal(200) = 1) then    dbms_output.put_line('工资高于平均工资');  elsif (check_sal(200) = 2) then    dbms_output.put_line('工资等于平均工资');  else    dbms_output.put_line('工资低于平均工资');  end if;end;

 

转载于:https://www.cnblogs.com/yaoweber/p/3950046.html

你可能感兴趣的文章
Java8 - 日期和时间实用技巧
查看>>
Java记录 -58- Iterator 迭代器
查看>>
RabbitMQ入门(5)--主题
查看>>
菜鸟如何使用GoEasy实现第一个web实时消息推送
查看>>
LNMMP架构的安装配置和功能的实现
查看>>
几个设置让你的邮箱不会爆满
查看>>
我的友情链接
查看>>
在linux6上安装RAC时多路径的权限设置
查看>>
[转载] 七龙珠第一部——第037话 忍者出现
查看>>
高并发—数据库自增主键解决(转)
查看>>
我的友情链接
查看>>
js判断滑动的距离 设置css
查看>>
Cisco路由器auto secure命令小结
查看>>
嵌入式Linux ARM汇编(二)——ARM异常处理
查看>>
手机淘宝客应用(服务端+客户端+数据库)源码项目
查看>>
Java基础学习总结(15)——java读取properties文件总结
查看>>
Modernizr——为HTML5和CSS3而生!
查看>>
Keealived+LVS 维护技巧与调优
查看>>
gnome桌面的美化
查看>>
脚踏实地向前走
查看>>