一、PL/SQL简介

  1、概念:PL/SQL是Oracle在标准SQL语言上的过程性扩展。

  2、优点和特性

  •   提高应用程序的运行性能
  •   提供模块化的程序设计功能
  •   允许定义标示符
  •   具有过程语言控制结构
  •   具备良好的兼容性
  •   处理运行错误

二、PL/SQL语言基础

  1、编写PL/SQL应用程序时,只能嵌入select语句、DML语句和事务控制语句,不能嵌入DDL语句,DCL语句。

  2、PL/SQL块

  基本结构如下所示:

 declare
/*定义部分-变量、常量、游标、异常、例解*/
begin
/*执行部分-PL/SQL,SQL 语句*/
exception
/*异常处理部分-处理运行错误*/
end;//块结束标志

  3、PL/SQL数据类型(常用)

  •   标量类型(常用char,varchar2,number,date,timestamp,boolean)
  •   属性类型(常用%type,%rowtype)
  • 其他record,table,varray

  4、注释:"--"单行注释,"/*...*/"多行注释

三、PL/SQL控制结构

  1、条件分支语句

  用法如下所示:

 1 --示例一
2 declare
3 v_sal number(6,2);
4 begin
5 select sal into v_sal from scott.emp where ename=trim('&ename');
6 if v_sal<2000 then
7 update scott.emp set sal=v_sal+200 where ename=trim('&ename');
8 end if;
9 end;
10
11 --示例二
12 declare
13 v_dep number(6,2);
14 v_sal number(6,2);
15 begin
16 select deptno,sal into v_dep,v_sal from scott.emp where ename=trim('&ename');
17 if v_dep=10 then
18 update scott.emp set sal=v_sal+200 where deptno=10;
19 elsif v_dep=20 then
20 update scott.emp set sal=v_sal+100 where deptno=20;
21 else
22 update scott.emp set sal=v_sal+50 where deptno!=10 and deptno!=20;
23 end if;
24 end;

  2、case语句

  具体用法如下所示:

 1 --示例一
2 declare
3 v_deptno scott.emp.deptno%type;
4 begin
5 v_deptno:=&deptno;
6 case v_deptno
7 when 10 then
8 update scott.emp set comm=100 where deptno=v_deptno;
9 when 20 then
10 update scott.emp set comm=80 where deptno=v_deptno;
11 when 30 then
12 update scott.emp set comm=60 where deptno=v_deptno;
13 else
14 dbms_output.put_line('该部门不存在!');
15 end case;
16 end;
17
18 --示例二
19 declare
20 v_sal scott.emp.sal%type;
21 v_ename scott.emp.ename%type;
22 begin
23 select ename,sal into v_ename,v_sal from scott.emp where empno=&empno;
24 case
25 when v_sal<2000 then
26 update scott.emp set comm=100 where ename=v_ename;
27 when v_sal<3000 then
28 update scott.emp set comm=80 where ename=v_ename;
29 when v_sal<4000 then
30 update scott.emp set comm=50 where ename=v_ename;
31 end case;
32 end;

  3、循环语句

  具体用法如下所示:

 1 --基本循环
2 declare
3 i int:=1;
4 begin
5 loop
6 dbms_output .put_line(i);
7 i:=i+1;
8 exit when i=10;
9 end loop;
10 end;
11
12--while循环
13 declare
14 i int:=1;
15 begin
16 while i<=10 loop
17 dbms_output.put_line(i);
18 i:=i+1;
19 end loop;
20 end;
21
22--for循环
23 declare 25 begin
26 for i in 1..10 loop
27 dbms_output.put_line(i);
28 end loop;
29 end;

四、异常处理

  1、分类:预定义异常,非预定义异常,自定义异常  

  2、常见预定义异常

  •   case_not_found:when子句中没包含必须的条件分支,又无else子句时触发。
  •   cursor_already_open:打开已打开的游标时触发。
  •   invalid_number:字符转换为数字失败时触发。
  •   too_many_rows:执行select into 子句时,返回超过一行触发。
  •   zero_divide:除数为0时触发。
  •   no_data_found:执行select into未返回行,或者引用了索引表未初始化元素时触发。

  具体用法如下所示:

 1 --预定义异常处理
2 eclare
3 v_ename scott.emp.ename%type;
4 begin
5 select ename into v_ename from scott.emp where empno=&empno;
6 dbms_output.put_line(v_ename);
7 exception
8 when no_data_found then
9 dbms_output.put_line('员工号不存在');
10 end;
11
12 --自定义异常处理
13 declare
14 e_integerity exception;--定义非预定义异常
15 e_no_employee exception;--定义自定义异常
16 pragma exception_init(e_integerity,-2291);--关联非预定义异常
17 begin
18 update scott.emp set deptno=40 where empno=-1;
19 if sql%notfound then
20 raise e_no_employee;--显示触发自定义异常
21 end if;
22 exception
23 when e_integerity then
24 dbms_output.put_line('该部门不存在!');
25 when e_no_employee then
26 dbms_output.put_line('该员工不存在!');
27 end;

五、游标(处理select语句返回的多行数据)

  (1)显示游标(查询结构超过一行)在PL/SQL块的声明部分申明,在执行部分或异常处理部分打开游标,提取数据,关闭游标。

  1、定义游标(declare cursor cursor_name is select_statement;)

  2、打开游标(open cursor_name;)

  3、提取数据

1 --每次提取一行
2 fetch cursot_name into variable1,variable2,..--variable :接收游标数据的变量
3--每次提取多行
4 .或 fetch cursor_name into bulk collect into collect1,collect2...--collect :接收游标结果的集合变量

  4、关闭游标(close cursor_name;)

  5、显示游标属性

  •   %isopen:判断游标是否打开,打开返回true,否则false;
  •   %found:是否从结果集中提取了数据。提取了数据返回true,否则false;
  • %not found:与%found相反;
  • rowcount:当前行数;

  具体用法如下所示:

 1 declare
2 cursor emp_cursor
3 is
4 select ename,sal from scott.emp where deptno=20;
5 v_ename scott.emp.ename%type;
6 v_sal scott.emp.sal%type;
7 begin
8 open emp_cursor;
9 loop
10 fetch emp_cursor into v_ename,v_sal;
11 exit when emp_cursor%notfound;
12 dbms_output.put_line(v_ename||':'||v_sal);
13 end loop;
14 close emp_cursor;
15 end;
16
17 --参数游标
18 declare
19 cursor emp_cursor(cno number)
20 is
21 select ename,sal from scott.emp where deptno=cno;
22 v_ename scott.emp.ename%type;
23 v_sal scott.emp.sal%type;
24 begin
25 if not emp_cursor%isopen then
26
27 open emp_cursor(10);
28 end if;
29 loop
30 fetch emp_cursor into v_ename,v_sal;
31 exit when emp_cursor%notfound;
32 dbms_output.put_line(v_ename||':'||v_sal);
33 end loop;
34 close emp_cursor;
35 end;
36
37 --使用游标更新或删除数据
38 declare
39 cursor emp_cursor
40 is
41 select ename,sal from scott.emp for update of sal;--在sal列上加上共享锁
42 v_ename scott.emp.ename%type;
43 v_sal scott.emp.sal%type;
44 begin
45 open emp_cursor;
46 loop
47 fetch emp_cursor into v_ename,v_sal;
48 exit when emp_cursor%notfound;
49 if v_sal<2500 then
50 update scott.emp set sal=sal+150 where current of emp_cursor;
51 end if;
52 end loop;
53 close emp_cursor;
54 end;
55 --游标的for循环语法
56 for record_name in curso_name loop
57 statement1;
58 statement2;
59 ...
60 end loop; --record_name:Oracle 隐式定义的记录变量名
61 --游标的for循环
62 declare
63 cursor emp_cursor
64 is select ename from scott.emp where deptno=20;
65 begin
66 for o in emp_cursor loop
67 dbms_output.put_line('第'||emp_cursor%rowcount ||'员工'||o.ename);
68 end loop;
69 end;
70
71
72 --不使用游标属性的for循环
73 begin
74 for o in (select ename from scott.emp where deptno=20)
75 loop
76 dbms_output.put_line(o.ename);
77 end loop;
78 end;
79
80 select * from scott.emp;

  

Oracle PL/SQL编程的更多相关文章

  1. ORACLE PL/SQL编程详解

    ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...

  2. [强烈推荐]ORACLE PL/SQL编程详解之七:程序包的创建与应用(聪明在于学习,天才在于积累!)

    原文:[强烈推荐]ORACLE PL/SQL编程详解之七:程序包的创建与应用(聪明在于学习,天才在于积累!) [强烈推荐]ORACLE PL/SQL编程详解之七: 程序包的创建与应用(聪明在于学习,天 ...

  3. ORACLE PL/SQL编程之八:把触发器说透

    原文:ORACLE PL/SQL编程之八:把触发器说透 ORACLE PL/SQL编程之八: 把触发器说透 大家一定要评论呀,感谢!光发表就花了我将近一个下午. 本篇主要内容如下: 8.1 触发器类型 ...

  4. [推荐]ORACLE PL/SQL编程之五:异常错误处理(知已知彼、百战不殆)

    原文:[推荐]ORACLE PL/SQL编程之五:异常错误处理(知已知彼.百战不殆) [推荐]ORACLE PL/SQL编程之五: 异常错误处理(知已知彼.百战不殆) 继上三篇:ORACLE PL/S ...

  5. ORACLE PL/SQL编程之六:把过程与函数说透(穷追猛打,把根儿都拔起!)

    原文:ORACLE PL/SQL编程之六:把过程与函数说透(穷追猛打,把根儿都拔起!) ORACLE PL/SQL编程之六: 把过程与函数说透(穷追猛打,把根儿都拔起!)   继上篇:ORACLE P ...

  6. [推荐]ORACLE PL/SQL编程详解之三:PL/SQL流程控制语句(不给规则,不成方圆)

    原文:[推荐]ORACLE PL/SQL编程详解之三:PL/SQL流程控制语句(不给规则,不成方圆) [推荐]ORACLE PL/SQL编程详解之三: PL/SQL流程控制语句(不给规则,不成方圆) ...

  7. [推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到)

    原文:[推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到) [推荐]ORACLE PL/SQL编程之四: 把游标说透(不怕做不到,只怕想不到) 继上两篇:ORACLE PL ...

  8. 【强烈强烈推荐】《ORACLE PL/SQL编程详解》全原创(共八篇)--系列文章导航

    原文:[强烈强烈推荐]<ORACLE PL/SQL编程详解>全原创(共八篇)--系列文章导航 <ORACLE PL/SQL编程详解> 系列文章目录导航 ——通过知识共享树立个人 ...

  9. [推荐]ORACLE PL/SQL编程详解之一:PL/SQL 程序设计简介(千里之行,始于足下)

    原文:[推荐]ORACLE PL/SQL编程详解之一:PL/SQL 程序设计简介(千里之行,始于足下) [推荐]ORACLE PL/SQL编程详解之一: PL/SQL 程序设计简介(千里之行,始于足下 ...

  10. [顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)

    原文:[顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功) [顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日 ...

随机推荐

  1. postman VS restlet client基本使用

    postman与restlet都是使用的google浏览器的插件(出不去自行解决,you get!),此两款软件的强大这里就不在赘述了,postman的网上说明很多,restlet的中文配置很少了.这 ...

  2. 采用API将AR应收账款未知未核销状态变成黄金

    DECLARE p_api_version NUMBER; p_init_msg_list VARCHAR2(200); p_commit VARCHAR2(200); p_validation_le ...

  3. 学习 protobuf(一)—— ubuntu 下 protobuf 2.6.1 的安装

    下载地址:https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz(如果初次下载失败,不妨多试 ...

  4. python 整数中1出现的次数

    把整数转换为字符串 用count计数 # -*- coding:utf- -*- class Solution: def NumberOf1Between1AndN_Solution(self, n) ...

  5. C# WPF QQ新消息托盘悬浮窗效果实现

    原文:C# WPF QQ新消息托盘悬浮窗效果实现 今天在做一个项目的时候需要这么一个效果,但是网上找了一会发现并没有现成的给我参考(复制),但是呢,我千(到)辛(处)万(抄)苦(袭)想(复)破(制)头 ...

  6. 向西项目管理工具Git一片

    前言 Git 这个词相信大家并不陌生,做开发的童鞋们每天都离不开它.当然,假设你的项目中没实用到分布式,那么,你可能从未用过 Git,当然也可能没听过.只是,这不是重点,重点是这一篇文章,我们将一起谈 ...

  7. poj 1125 Stockbroker Grapevine(多源最短)

    id=1125">链接:poj 1125 题意:输入n个经纪人,以及他们之间传播谣言所需的时间, 问从哪个人開始传播使得全部人知道所需时间最少.这个最少时间是多少 分析:由于谣言传播是 ...

  8. 使用 Capistrano 和写作 Ruby 迭代边缘部署

    想边自己写ruby代码,边部署随时能够到处查看,heroku域名又不友好,速度在国内又慢.于是乎想起来capistrano,于是学起 ... capistrano 一点入门认知 https://www ...

  9. Android Training - 使用IntentService运行任务(Lesson 1 - 创建IntentService)

    写在http://hukai.me/blog/android-training-18-running-background-service-lesson-1/ 版权声明:本文博客原创文章,博客,未经同 ...

  10. 解压压缩文件报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now

    压缩包是直接weget 后面加官网上的tar包地址获取的  [root@xuegod43 ~]# tar -zxvf /home/hadoop/hadoop-2.6.5-src.tar.gz gzip ...