Oracle PL/SQL编程

   2023-02-10 学习力0
核心提示:一、PL/SQL简介  1、概念:PL/SQL是Oracle在标准SQL语言上的过程性扩展。  2、优点和特性  提高应用程序的运行性能  提供模块化的程序设计功能  允许定义标示符  具有过程语言控制结构  具备良好的兼容性  处理运行错误二、PL/SQL语言基础 

一、PL/SQL简介

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

  2、优点和特性

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

二、PL/SQL语言基础

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

  2、PL/SQL块

  基本结构如下所示:

1 declare 
2 /*定义部分-变量、常量、游标、异常、例解*/
3 begin
4 /*执行部分-PL/SQL,SQL 语句*/
5 exception
6     /*异常处理部分-处理运行错误*/
7 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;

 

 

 

 

  

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • sql:mysql:函数:TIMESTAMPDIFF函数实现TimeStamp字段相减,求得时间差
    sql:mysql:函数:TIMESTAMPDIFF函数实现TimeS
     函数内指定是minute,则最终结果value值的单位是分钟,如果函数内指定为hours,则最终结果value值单位为小时。//UPLOAD_TIME 减去 CREATE_DTTM 求得时间差,以分钟数计时select avg(TIMESTAMPDIFF(MINUTE,CREATE_DTTM,UPLOAD_TIME)) value,LEFT(CREATE_DTTM
    03-08
  • 去重复的sql(Oracle) 去重复的英文
    1.利用group by 去重复2.可以利用下面的sql去重复,如下  1) select id,name,sex from (select a.*,row_number() over(partition by a.id,a.set order by name) su from test a ) where su=1  2)select id,name,sex from (select a.*,row_number() over(p
    02-10
  • Oracle SQL七次提速技巧
    以下SQL执行时间按序号递减。1,动态SQL,没有绑定变量,每次执行都做硬解析操作,占用较大的共享池空间,若共享池空间不足,会导致其他SQL语句的解析信息被挤出共享池。create or replace procedure proc1as beginfor i in 1..100000 loop    execute imme
    02-10
  • Oracle\SQL  Server等及其他基本语句写法
    Oracle\SQL Server等及其他基本语句写法
    Oracle\SQL  Server等及其他基本语句写法目录一.Excel相关 11.Excel中写脚本范例: 12.提取字节 23. 提取单元格内字符 24.VLOOKUP函数: 2二.SQL语句汇总 21.建表: 22.增 33.删 44.查 65.改 236.Alter的应用 24三.数据库备份与恢复脚本 261. Oracle: 2
    02-10
  • SQL ORACLE case when函数用法
    case when 用法(1)简单case函数:格式:  case 列名   when 条件值1 then 选项1  when 条件值1 then 选项2......  else 默认值 end例如:  select   case job_level  when '1' then '1111'  when '2' then '2222'   when '3' then '3333
    02-10
  • mysql下如何执行sql脚本 执行SQL脚本
    1.编写sql脚本,假设内容如下:  create database dearabao;  use dearabao;  create table niuzi (name varchar(20));  保存脚本文件,假设我把它保存在F盘的hello world目录下,于是该文件的路径为:F:\hello world\niuzi.sql2.执行sql脚本,可以有2种方法: 
    02-10
  • MySQL 5.7版本sql_mode=only_full_group_by问题
    用到GROUP BY 语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'col_user_6.a.START_TIME' which is not functionally dependent on colu
    02-10
  • Oracle迁移到MySQL性能下降的注意点 oracle数据
    背景:最近有较多的客户系统由原来由Oracle改造到MySQL后出现了性能问题CPU 100%,或是后台的CRM系统复杂SQL在业务高峰的时候出现堆积导致业务故障。在我的记忆里面淘宝最初从Oracle迁移到MySQL期间也遇到了很多SQL的性能问题,记忆最为深刻的子查询,当初的
    02-10
  • ORACLE中通过SQL语句(alter table)来增加、删除
    1.添加字段:alter table  表名  add (字段  字段类型)  [ default  '输入默认值']  [null/not null]  ;2.添加备注:comment on column  库名.表名.字段名 is  '输入的备注';  如: 我要在ers_data库中  test表 document_type字段添加备注  comm
    02-10
  • MySQL与Oracle 差异比较之六触发器
    触发器编号类别ORACLEMYSQL注释1创建触发器语句不同create or replace trigger TG_ES_FAC_UNIT  before insert or update or delete on ES_FAC_UNIT  for each rowcreate trigger `hs_esbs`.`TG_INSERT_ES_FAC_UNIT` BEFORE INSERT on `hs_esbs`.`es_fac_u
    02-10
点击排行