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

1. 是被命名的pl/sql块

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

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

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

必须有执行部分

可选的异常处理部分

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

三、存储过程:语法

  1. CREATE [OR REPLACE] PROCEDURE procedure_name
  2. [(argument1 [mode1] datatype1,
  3. argument2 [mode2] datatype2,
  4. . . .)]
  5. IS|AS
  6. procedure_body;
  1. create or replace procedure add_dept is
  2. v_deptment_id dept.deptno%type;
  3. v_deptment_name dept.dname%type;
  4. begin
  5. v_deptment_id :=;
  6. v_deptment_name := 'YWB';
  7. insert into dept(deptno,dname) values(v_deptment_id,v_deptment_name);
  8. commit;
  9. dbms_output.put_line('插入了:'||sql%rowcount||'行');
  10. end;

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

  1. begin
  2. add_dept;
  3. end;

四、函数:

  1. CREATE [OR REPLACE] FUNCTION function_name
  2. [(argument1 [mode1] datatype1,
  3. argument2 [mode2] datatype2,
  4. . . .)]
  5. RETURN datatype
  6. IS|AS
  7. function_body;

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

  1. create or replace function check_sal return boolean is
  2. dept_id emp.deptno%type :=;
  3. emp_no emp.empno%type :=;
  4. salary emp.sal%type;
  5. avg_sal emp.sal%type;
  6. begin
  7. select sal into salary from emp where empno=emp_no;
  8. select avg(sal) into avg_sal from emp where deptno=dept_id;
  9. if salary>avg_sal then
  10. return true;
  11. else
  12. return false;
  13. end if;
  14. exception
  15. when no_data_found then
  16. return null;
  17. end;

在匿名块中调用函数:

  1. begin
  2. if( check_sal is null) then
  3. dbms_output.put_line('由于程序异常,输出null');
  4. elsif (check_sal) then
  5. dbms_output.put_line('工资高于平均工资');
  6. else
  7. dbms_output.put_line('工资低于平均工资');
  8. end if;
  9. end;

给函数传递参数:

  1. create or replace function check_sal(empno number) return boolean is
  2. dept_id employees.department_id%type;
  3. sal employees.salary%type;
  4. avg_sal employees.salary%type;
  5. begin
  6. select salary, department_id
  7. into sal, dept_id
  8. from employees
  9. where employee_id = empno;
  10. select avg(salary)
  11. into avg_sal
  12. from employees
  13. where department_id = dept_id;
  14. if sal > avg_sal then
  15. return true;
  16. else
  17. return false;
  18. end if;
  19. exception
  20. when no_data_found then
  21. return null;
  22. end;
  23.  
  24. create or replace function check_sal(empno number) return number is
  25. dept_id employees.department_id%type;
  26. sal employees.salary%type;
  27. avg_sal employees.salary%type;
  28. begin
  29. select salary, department_id
  30. into sal, dept_id
  31. from employees
  32. where employee_id = empno;
  33. select avg(salary)
  34. into avg_sal
  35. from employees
  36. where department_id = dept_id;
  37. if sal > avg_sal then
  38. return ;
  39. elsif (sal = avg_sal) then
  40. return ;
  41. else
  42. return ;
  43. end if;
  44. exception
  45. when no_data_found then
  46. return null;
  47. end;
  48.  
  49. begin
  50. if (check_sal() is null) then
  51. dbms_output.put_line('由于程序异常,输出NULL');
  52. elsif (check_sal() = ) then
  53. dbms_output.put_line('工资高于平均工资');
  54. elsif (check_sal() = ) then
  55. dbms_output.put_line('工资等于平均工资');
  56. else
  57. dbms_output.put_line('工资低于平均工资');
  58. end if;
  59. end;

创建存储过程和函数【weber出品必属精品】的更多相关文章

  1. 全世界最详细的图形化VMware中linux环境下oracle安装(二)【weber出品必属精品】

    <ORACLE 10.2.05版本的升级补丁安装> 首先我们解压 $ unzip p8202632_10205_LINUX.zip 解压后我们会发现多出了个文件夹,他是:Disk1,进入D ...

  2. 全世界最详细的图形化VMware中linux环境下oracle安装(一)【weber出品必属精品】

    安装流程:前期准备工作--->安装ORACLE软件--->安装升级补丁--->安装odbc创建数据库--->安装监听器--->安装EM <前期准备工作> 安装 ...

  3. 创建和管理表【weber出品必属精品】

    创建表 必须有 : 1. CREATE TABLE 的权限 SQL> conn /as sysdba 已连接. SQL> create user test default tablespa ...

  4. ORACLE SQL 组函数【weber出品必属精品】

    组函数:对一组数据进行加工,每组数据返回一个值 常用的组函数:count()  avg()  max()   min()  sum()   count()函数  1. count(*) :返回总共的行 ...

  5. ORACLE SQL单行函数(三)【weber出品必属精品】

    16.L:代表本地货币符,这个和区域有关.这个时候我们想来显示一下人民币的符号:¥ $ vi .bash_profile ---写入如下内容: export NLS_LANG='SIMPLIFIED ...

  6. ORACLE SQL单行函数(二)【weber出品必属精品】

    11.dual:虚表,任何用户都可以使用,表结构如下: SQL> desc dual Name Null? Type -------------------------------------- ...

  7. ORACLE SQL单行函数(一)【weber出品必属精品】

    1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...

  8. 数据库对象(视图,序列,索引,同义词)【weber出品必属精品】

    视图视图的定义:视图就是一个查询的别名为什么使用视图限制数据的存取 SQL> conn /as sysdba 已连接. SQL> grant create view to scott; 授 ...

  9. 使用DML语句【weber出品必属精品】

    DML语句包含以下语法: INSERT:往一个表中增加新行 DELETE:从一个表中删除掉现有的行 UPDATE:更改一个表中现有的行 INSERT语句语法:INSERT INTO TABLE(COL ...

随机推荐

  1. for循环删除list元素陷阱

    首先我们先看一段代码,如下: List<String> list=new ArrayList<String>(); list.add("123"); lis ...

  2. uva 10963 - The Swallowing Ground

    #include <iostream> #include <cstdio> #include <cstdlib> #include <set> usin ...

  3. memcached学习笔记——存储命令源码分析下篇

    上一篇回顾:<memcached学习笔记——存储命令源码分析上篇>通过分析memcached的存储命令源码的过程,了解了memcached如何解析文本命令和mencached的内存管理机制 ...

  4. ubuntu下安装pyqt5

    在网上看了很多ubuntu系统中安装pyqt5,感觉有些麻烦. 主要的库只有一个:python3-pyqt5 可通过新立得安装,也可通过shell命令安装 sudo apt-get install p ...

  5. mac下 配置tomcat

    第一步: 1.打开你的终端:然后输入  pico .bash_profile   回车 第二步: 2. 然后添加你tomcat放入的路径的path 编辑完后,control+x   (保存)    继 ...

  6. Android 颜色大全 (colors.xml )

    <resources> <color name="pink">#ffc0cb</color><!--粉红色 --> <colo ...

  7. CSS3 Flexbox布局那些事

    相信研究过CSS3的同学对Flexbox布局一定不会陌生(作为一个未来主流的布局方式,至少有所耳闻).最近完成了两个项目:一个是移动端H5项目,一个是嵌入HTML页面的mac客户端项目.为了庆祝这两个 ...

  8. rsyslog的ommsql模块如何连接MYSQL的非标准数据库端口?

    搞了我半小个时查找资料..最后,在一个官方文档中找到他... http://www.rsyslog.com/doc/ommysql.html Sample: The following sample ...

  9. UML--对象的介绍

    UML相对于学习UML的符号含义而言,掌握它们背后的方法和思想是更为重要的.软件是一种实践知识,仅仅靠书本不可能成为高手.书本只能给出思路和知识点,而掌握和消化这些知识则必须在实践中去完成. 如果我们 ...

  10. dataList中实现用复选框一次删除多行问题

    先遍历每一行,判断checkBox是否选中,再获取选中行的主键Id 删除就行了 ,,,foreach(DatalistRow rowview in Datalist.Rows) //遍历Datalis ...