/*
=============================================================================pl/sql编程
=============================================================================*/--pl/sql块的结构
declare --声明部门:在此声明pl/sql用到的变量、类型以及游标,以及局部的存储过程和函数
begin--执行部分:过程及sql语句,即程序的组成部分
exception --异常处理部分:错误处理
end;--例子
create table toys
(
id number(20),
name varchar2(50),
price number(5,2),
sal_date date
)
insert into toys values(1,'张三',525,sysdate)
insert into toys values(2,'李四',525,'2016-05-06');
select * from toys;declare v_name varchar2(20);v_price number;
beginselect name,price into v_name,v_price from toys where id=1;dbms_output.put_line('名字:'||v_name||',价格'||v_price);
end;
10/*
type和rowtype
-------------------------------------------------
*/
declare -- v_name varchar2(25);-- v_name1 toys.name%type;   --返回一个v_name1 toys%rowtype;  --返回多个e_ronull exception;--声明异常
begin select * into v_name1 from toys where id=1;dbms_output.put_line('名字:'||v_name1.name);
exception when dlp_val_on_index thendbms_output.put_line('将重复键插入id列');
end;--税点
declare
v_start constant number:=3500;   --声明常量
begin
--sql语句
end--常量和变量的声明变量名称 pl/sql的数据类型(大小):=init_value;
eg:variable_name constant data_type:=value;--应用实例
declare v_ename varchar2(20);v_rate number(7,2);c_rate_incr constant number(7,2):=1.10;
begin--方法一:通过select into 赋值select ename,sal* c_rate_incr into v_ename,v_rate from employee where empno='7788';--方法二:通过赋值操作符“:=”给变量赋值v_ename:='scott';
end;--使用序列赋值v_no:=emp_seq.nextval;----------------------实例2---------------------------------
--根据员工编号查询员工信息
declare v_empno employee.empno%type:=4;v_rec employee%rowtype;
beginselect * into v_rec from employee where empno=v_empno;dbms_output.put_line('姓名:'||v_rec.ename||'工资:'||v_rec.sal||'工作时间:'||v_rec.hiredate);end;
--==执行成功之后,输出:姓名:张四工资:10000工作时间:2017-02-02 00:00:00/*
----------------------pl/sql控制语句--------------------------------
*/
--if的语法
if <布尔表达式> thenpl/sql和sql语句
end if;
------------------------
if<布尔表达式> thenpl/sql和sql语句
else其他语句
end if;
-------------------------
if <布尔表达式> thenpl/sql语句和sql语句
elsif <其他布尔表达式> then其他语句
elsif <其他布尔表达式> then其他语句
else其他语句
end if;----注意:是elsif  不是elseif------------------------------case的语法--------------------------
-------格式一------
case 条件表达式when 条件表达式结果1 then语句段1when 条件表达式结果2 then语句段2when 条件表达式结果n then语句段n[else语句段]
end case;
-------格式二------
casewhen 条件表达式1 then语句段1when 条件表达式2 then语句段2when 条件表达式n then语句段n
else 语句段
end case;------------------------------循环控制--------------------------
loop 要执行的语句;exit when <条件语句>  --条件满足时,退出循环语句
end loop;---while循环语句的语法
while <布尔表达式> loop 要执行的语句;
end loop;
--for循环语句的语法
for 循环计数器 in [reverse] 下限 ...上限 loop要执行的语句
end loop;------------------------------实例3-------------------------
declare v_counter number:=5;
begindbms_output.put_line('v_counter的当前值为:'||v_counter);if v_counter>=10 thennull;--为了使语法变得有意义,去掉null会报语法错误elsev_counter:=v_counter+10;dbms_output.put_line('v_counter 的改变值为:'||v_counter);end if;end;--========执行成功之后输出:v_counter的当前值为:5   v_counter 的改变值为:15/*=======================异常处理机制===============================
*/--语法
begin sequence_of_statements;
exception when <exception_name> thensequence_of_statements;when others thensequence_of_statements;
end;----------------------------实例4------------------------------------
--查询编号为7788的雇员的福利补助(comm列)
declare v_comm employee.comm%type;e_comm_is_null exception ;--定义异常类型变量
begin select comm into v_comm from employee where empno=7788;if v_comm is null thenraise e_comm_is_null;end if;
exception when no_data_found thendbms_output.put_line('雇员不存在!错误为:'||sqlcode||sqlerrm);when e_comm_is_null thendbms_output.put_line('该雇员无补助');when others then dbms_output.put_line('出现其他异常!');end;
----================测试运行结果:雇员不存在!错误为:100ORA-01403: 未找到任何数据--自定义异常
raise_application_error(error_number,error_message);--实例
declare ....begin ....if v_com is null thenraise_application_error(-20001,'该雇员无补助');end if;
end;/*
====================================显示游标================================
*/
--1.声明游标
cursor cursor_name [(parameter [,parameter]...)]
[return return_type] is select_statement;
--2.打开游标
open cursor_name[(parameters)];
--3.提取游标
fetch cursor_name into variables;
--4.关闭游标
close cursor_name;--------------------实例6------------------------
declare name employee.ename%type;sal employee.sal%type;   --定义两个变量来存放ename和sal的内容cursor emp_cursor is select ename,sal from employee;
beginopen emp_cursor;loopfetch emp_cursor into name,sal;exit when emp_cursor%notfound;dbms_output.put_line('第'||emp_cursor%rowcount||'个雇员:'||name|| 'oooo' || sal);end loop;close emp_cursor;
end;--===执行成功输出:
/*
第1个雇员:张一3000
第2个雇员:张二5000
第3个雇员:张三8000
第4个雇员:张四10000
第5个雇员:张五6300*/--使用显示游标删除或者更新
cursor cursor_name id select_statement for update [of columns];
--在使用for update 子句声明游标时,可以使用下面语法更新行
update table_name set column_name=column_value where current of cursor_name;--根据编号查询雇员的姓名
declare v_ename varchar2(20);
beginselect ename into v_ename from employee where empno=&empno;dbms_output.put_line('雇员的名字是:'||v_ename);
end;select * from employee;

PL/SQL编程基本概念相关推荐

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

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

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

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

  3. ORACLE PL/SQL编程

    PL/SQL程序设计 什么是PL/SQL PL/SQL是 Procedure Language & Structured Query Language 的缩写.PL/SQL是对SQL语言存储过 ...

  4. Oracle PL/SQL编程详解

    Oracle PL/SQL编程详解 - 古立 - 博客园 <我的网络摘抄本> 网摘/转载/备忘/随记 博客园 首页 新随笔 联系 管理 订阅 随笔- 84  文章- 0  评论- 0  & ...

  5. Oracle学习笔记(最重要的是PL/SQL编程)

    一:Oracle认证,与其它数据库比较,安装 Oracle安装会自动的生成sys用户和system用户: (1) sys用户是超级用户,具有最高权限,具有sysdba角色,有create databa ...

  6. Oracle的PL/SQL编程前奏之基础技能实战一(匿名子程序)

    Oracle的PL/SQL编程之基础技能实战一 一>基础代码检查检查以bm_开头的系统初始化编码表是否有空值.与业务系统相关的编码项不能存在空值,会导致系统业务无法办理.为初始化数据表.在做测试 ...

  7. pl/sql编程基础

    PL/SQL 1.过程.函数.触发器是pl/sql编写的 2.过程.函数.触发器是存放在oracle数据库中的 3.pl/sql是非常强大的过程化语言 4.过程.函数.触发器可以在java程序中调用 ...

  8. oracle pl/sql编程详细,Oracle框架:PL/SQL编程:

    PL/SQL编程 一:什么是PL/SQL (1.)PL/SQL体系结构: PL/SQL引擎用来编译和执行,PL/SQL块或子程序,该引擎驻留在Oracle服务器中. (2.)PL/SQL块简介 PL/ ...

  9. 10、oracle下PL/SQL编程基础

    ORACLE下的PL/SQL编程基础 PL/SQL语言是程序化程序设计语言,块是PL/SQL编程中的基本结构,其优点在于支持SQL.支持面向对象编程.性能好.可移植性.与sql集成.安全性高等. 1. ...

最新文章

  1. pandas dataframe数据聚合groupby、agg、privot基于sum统计详解及实例
  2. IOS固定IP对动态IP用pre-share
  3. 浅析Codewarrior、IAR和Keil MDK三大开发环境优缺点
  4. 前端性能优化(PC版)
  5. 图片列表页的显示方法
  6. 解决qt程序运行时的cannot create Qt for Embedded Linux data directory: /tmp/qtembedded-0
  7. Android 驱动测试程序H-M-S 6
  8. r9270公版bios_显卡成功刷入UEFI GOP BIOS 彻底解决开机扁苹果
  9. GSM/CDMA/GPRS介绍
  10. 紫薇在线排盘php源码,灵匣网紫微斗数在线排盘系统
  11. 在线旅游OTA行业调研报告-携程美团同程飞猪booking对比分析
  12. 暗黑管理系列:发红包的管理杠杆率和量级作用
  13. 保留数据和程序win7升级win10,平滑升级,完美!
  14. JWT简介、JWT优缺点、JWT使用方法、.NET6使用JWT示例、JWT与Session对比
  15. Comparable 和 Comparator 比较器
  16. 网络转载——人生最重要的三种能力,不是读书能学来的!
  17. 二、详解 DVWA_Reflected反射型XSS
  18. 怎么用域名访问网站?
  19. 如何使用vue-cli搭建SPA项目
  20. 【区块链】走进web3的世界-gas费用

热门文章

  1. 操作系统习题——(习题二)
  2. Redis底层实现--字符串
  3. matlab title多个标题_MATLAB中的直方图处理及均衡化
  4. caffe blob操作
  5. 深入理解 JVM Class文件格式(六)
  6. 世界五星级大厨经典菜品集
  7. 【无码专区8】三角形二维数点——计数有多少个给定点落在三角形区域内
  8. CF1580B Mathematics Curriculum(笛卡尔树、树形dp)
  9. CF1556F-Sports Betting【状压dp,数学期望】
  10. P6855-「EZEC-4.5」走方格【dp】