oracle系列(四)PL/SQL
过程,函数,触发器是PL/SQL编写的,存储在oracle中的.
PL/SQL是非常强大的数据库过程语言.
PL/SQL优点:
性能,模块化,网络传输量,安全性
缺点:移植性不好
简单分类:
块:过程,函数,触发器,包
Demo:
create or replace procedure sp01 is
begin
insert into ice.persons values(10,'jack',18,'shenzhen');
commit;
end sp01;
编写规范:
1.注释
单行注释 --
多行注释 /*... */
2.标识符号的命名规范
1)变量,v_作为前缀 v_sal (变量名不要跟表的字段名一样)
2)常量,c_作为前缀 c_rate
3)游标,_cursor作为后缀 emp_cursor;
4)例外,e_作为前缀,e_error
table:persons
ID NAME AGE CITY
1 10 tom 18 shenzhen
--SQL Window
--1.0最简单的块
begin
dbms_output.put_line('hello world!');
end;
--包含定义部分和执行部分的pl/sql块
declare
v_ename varchar2(5); --定义字符串变量
v_city varchar2(50);
begin
select t.name,t.city into v_ename,v_city from PERSONS t where t.id = &person_id;
dbms_output.put_line('ID为10的Person的名字' || v_ename||',城市'||v_city);
--异常处理
exception
when no_data_found then
dbms_output.put_line('编号输入有误!');
end;
--2.0 过程
create procedure sp02(personId INTEGER, personName varchar2) is
begin
update persons p set p.name=personName where p.id=personId;
commit;
end;
--3.0 函数
create or replace function func_01(personId in varchar2) return number is
FunctionResult number;
begin
select p.age+18 into FunctionResult from ice.persons p where p.id=personId;
return(FunctionResult);
end func_01;
--调用
select func_01(10) from dual;
--4.0包
--4.1创建包规范
create or replace package pkg_01 is
-- Function and procedure implementations
function func_02(personId in varchar2) return number;
end pkg_01;
--4.2创建包体
create or replace package body pkg_01 is
-- Function and procedure implementations
function func_02(personId in varchar2) return number is
FunctionResult number;
begin
select p.age + 18
into FunctionResult
from ice.persons p
where p.id = personId;
return(FunctionResult);
end func_02;
end pkg_01;
--5.0定义并使用变量
--5.1标量
v_ename varchar2(10);
v_sal number(6,2);
v_sal2 number(6,2):=5.4;--注意赋值运算符 :=
v_hiredate date;
v_valid boolean not null default false;
%type
v_ename persons.name%type;
--5.2记录类型
declare
type person_record_type is record(
name persons.name%type,
age persons.age%type);
sp_record person_record_type;
begin
select p.name, p.age into sp_record from persons p where p.id = 10;
dbms_output.put_line(sp_record.name || '->' || sp_record.age);
end;
--5.3表类型:相当于高级语言中的数组
declare
type sp_table_type is table of persons.name%type index by binary_integer;
sp_table sp_table_type;
begin
select name into sp_table(0) from persons p where p.id = 10;--去掉where会报错. 如果要去掉,使用参照变量
dbms_output.put_line('姓名' || sp_table(0));
end;
--5.4参照变量:游标变量(ref cursor)
declare
type sp_person_cursor_type is ref cursor;
person_cursor sp_person_cursor_type;
v_name persons.name%type;
v_age persons.age%type;
begin
open person_cursor for
select name, age from persons p;
loop
fetch person_cursor
into v_name, v_age;
--判断退出
exit when person_cursor%notfound;
dbms_output.put_line(v_name || '->' || v_age);
end loop;
end;
https://www.cnblogs.com/linjiqin/category/349944.html
oracle系列(四)PL/SQL的更多相关文章
- Oracle数据库之PL/SQL程序设计简介
PL/SQL程序设计简介 一.什么是PL/SQL? PL/SQL是 Procedure Language & Structured Query Language 的缩写. ORACLE的SQL ...
- 64位Oracle 11g 使用PL/SQL
Oracle 11g和PL/SQL安装完后,发现打开PL/SQL并不能连接Oracle数据库! [第一回合]完败! 先是在网上找解决方法,说是需要使用Net Configuration Assista ...
- oracle数据库之PL/SQL 块结构和组成元素
一.PL/SQL 块 (一)PL/SQL 程序由三个块组成,即声明部分.执行部分.异常处理部分 PL/SQL 块的结构如下: 1.DECLARE /* 声明部分: 在此声明 PL/SQL 用到的变量, ...
- ORACLE中的PL/SQL
一. 1.过程,函数,触发器是pl/sql编写. 2. 过程函数触发器是在Oracle中. 3.pl/sql是非常强大的数据库过 ...
- 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》
本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...
- 《oracle每日一练》免安装Oracle客户端使用PL/SQL
免安装Oracle客户端使用PL/SQL Oracle客户端挺招人烦的,部署连接它的应用通常需要先安装它的客户端,安装程序要求在目标机器上写注册表,假设你没有洁癖的话,你仍可能被下面的事情绊住:当你的 ...
- Oracle 客户端安装 + pl/sql工具安装配置
Oracle 客户端安装 + pl/sql工具安装配置 下载oracle客户端,并在本地安装. 11g下载地址为: http://www.oracle.com/technetwork/databas ...
- oracle instantclient basic +pl/sql 安装和配置
oracle instantclient basic +pl/sql 安装和配置 大家都知道,用PL/SQL连接Oracle,是需要安装Oracle客户端软件的,oracle客户端有点大,比较耗资源. ...
- Oracle数据库之PL/SQL触发器
Oracle数据库之PL/SQL触发器 1. 介绍 触发器(trigger)是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是 ...
随机推荐
- HTML总结摘要
一 概述 1.什么是HTML? HyperText Markup Language,超文本标记语言,客户端技术的技术,负责页面展示. 2.HTML的特点 标签不区分大小写. 3.请求地址 HTML是客 ...
- git之删除过滤
把不想提交的内容删除过滤 git rm --cached **/** -f
- Vue.js基础2
声明式渲染 Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM: <div id="app"> {{ message }} </div ...
- 创建线程后马上CloseHandle(threadhandle)起什么作用
原文:http://www.cnblogs.com/eddyshn/archive/2010/04/14/1711674.html HANDLE threadhandle = CreateThread ...
- linux 安装源码后的操作 ldconfig
https://blog.csdn.net/cqkxboy168/article/details/8657487 知识点: .如果使用 ldd 命令时没有找到对应的共享库文件和其具体位置,可能是两种情 ...
- libcurl 使用
关于libcurl的文章网络上很多, 这里不再描述. 以下是如何使用libcurl的例子. 一.常用函数 1) libcurl的全局初始化及释放 CURLcode curl_global_ ...
- Linux Firefox Adobe Flash Player 安装和更新
1.下载 Firefox Adobe Flash Player 使用Linux上的火狐浏览器访问如下的下载网址: https://get.adobe.com/flashplayer/ 选择下载 &qu ...
- maven学习(六)依赖、聚合、继承
先说一下概念(个人理解的,有问题请留言): 依赖:我要盖一座房子,就需要很多的砖,这些专就是盖房子的一个依赖.我要跑一个maven项目,需要各种各样的功能,功能实现的jar包和插件就是我的依赖. 聚合 ...
- C++ 类对象的初始化顺序 ZZ
C++构造函数调用顺序 1. 创建派生类的对象,基类的构造函数优先被调用(也优先于派生类里的成员类): 2. 如果类里面有成员类,成员类的构造函数优先被调用:(也优先于该类本身的构造函数 ...
- PPTP has become obsolete
https://www.ovpn.com/en/blog/pptp-has-become-obsolete/ PPTP has become obsolete What is PPTP? PPTP s ...