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

1. 是被命名的pl/sql块

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

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

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

必须有执行部分

可选的异常处理部分

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

三、存储过程:语法

CREATE [OR REPLACE] PROCEDURE procedure_name
[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
. . .)]
IS|AS
procedure_body;
create or replace procedure add_dept is
v_deptment_id dept.deptno%type;
v_deptment_name dept.dname%type;
begin
v_deptment_id :=;
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 datatype
IS|AS
function_body;

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

create or replace function check_sal return boolean is
dept_id emp.deptno%type :=;
emp_no emp.empno%type :=;
salary emp.sal%type;
avg_sal emp.sal%type;
begin
select 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 ;
elsif (sal = avg_sal) then
return ;
else
return ;
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('工资高于平均工资');
elsif (check_sal() = ) then
dbms_output.put_line('工资等于平均工资');
else
dbms_output.put_line('工资低于平均工资');
end if;
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. Linux下修改字符集,转自

    以下转自http://blog.csdn.net/cyuyan112233/article/details/6539122 Linux下修改字符集 locale -a 查询系统支持的字符集 expor ...

  2. OC基础 内存管理

    OC基础  内存管理 我们所了解的c语言内存管理,如下: (1)c语言的内存分配:char *p = (char*)malloc(100*sizeof(char)); (2)c语言的内存释放:free ...

  3. C#析构函数,类运行结束后运行

    public class Students { //创建对像时使用 public Students(string name, int age, char gender, int englist, in ...

  4. Word隐藏回车符技巧

    每一次在Word中敲击回车时,都会留下一个回车符,回车次数多了回车符也就跟着变多了,这的确是太影响视觉效果了,我们要如何操作才能将这些回车符去掉呢?特意为大家献上Word2003和Word2007中隐 ...

  5. 阿里大鱼simplexmlelement object 取值PHP

    SimpleXMLElement Object(    [code] => 15    [msg] => Remote service error    [sub_code] => ...

  6. HTML5比较实用的代码

    增强IE兼容性 <!--[if IE]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js&q ...

  7. 进程外组件通信之免注册com通信【原创】

    最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...

  8. PlayerPrefs类

    该类用于本地持久化保存与读取数据工作原理是:以键值对的形势将数据保存在文件中.该类可以保存与读取3种基本的数据类型,它们是浮点型.整型和字符串型,涉及的方法如下.SetFloat():保存浮点类型Se ...

  9. Cmake的install与file命令的区别

    实际上他们两个可以达到一个目标(对于文件操作),但是又有本质上的区别,文档没有细看,但是一般利于项目的管理,使用install,install命令如果在cmake命令中没有指名install参数,实际 ...

  10. 【转】关于Adapter的The content of the adapter has changed问题分析 关于Adapter的The content of the adapter has changed问题分析

    原文网址:http://www.cnblogs.com/monodin/p/3874147.html 1.问题描述 1 07-28 17:22:02.162: E/AndroidRuntime(167 ...