过程,函数,触发器是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的更多相关文章

  1. Oracle数据库之PL/SQL程序设计简介

    PL/SQL程序设计简介 一.什么是PL/SQL? PL/SQL是 Procedure Language & Structured Query Language 的缩写. ORACLE的SQL ...

  2. 64位Oracle 11g 使用PL/SQL

    Oracle 11g和PL/SQL安装完后,发现打开PL/SQL并不能连接Oracle数据库! [第一回合]完败! 先是在网上找解决方法,说是需要使用Net Configuration Assista ...

  3. oracle数据库之PL/SQL 块结构和组成元素

    一.PL/SQL 块 (一)PL/SQL 程序由三个块组成,即声明部分.执行部分.异常处理部分 PL/SQL 块的结构如下: 1.DECLARE /* 声明部分: 在此声明 PL/SQL 用到的变量, ...

  4. ORACLE中的PL/SQL

    一. 1.过程,函数,触发器是pl/sql编写.                2. 过程函数触发器是在Oracle中.                      3.pl/sql是非常强大的数据库过 ...

  5. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》

    本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...

  6. 《oracle每日一练》免安装Oracle客户端使用PL/SQL

    免安装Oracle客户端使用PL/SQL Oracle客户端挺招人烦的,部署连接它的应用通常需要先安装它的客户端,安装程序要求在目标机器上写注册表,假设你没有洁癖的话,你仍可能被下面的事情绊住:当你的 ...

  7. Oracle 客户端安装 + pl/sql工具安装配置

    Oracle 客户端安装 +  pl/sql工具安装配置 下载oracle客户端,并在本地安装. 11g下载地址为: http://www.oracle.com/technetwork/databas ...

  8. oracle instantclient basic +pl/sql 安装和配置

    oracle instantclient basic +pl/sql 安装和配置 大家都知道,用PL/SQL连接Oracle,是需要安装Oracle客户端软件的,oracle客户端有点大,比较耗资源. ...

  9. Oracle数据库之PL/SQL触发器

    Oracle数据库之PL/SQL触发器 1. 介绍 触发器(trigger)是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是 ...

随机推荐

  1. 洛谷P1072 Hankson 的趣味题(数学)

    题意 题目链接 Sol 充满套路的数学题.. 如果你学过莫比乌斯反演的话不难得到两个等式 \[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\] \[gcd(\frac ...

  2. HTML颜色代码

    记录十种个人比较喜欢的颜色: #19CAAD   #8CC7B5  #A0EEE1  #BEE7E9  #BEEDC7 #D6D5B7  #D1BA74  #E6CEAC  #ECAD9E  #F46 ...

  3. 属性只有一个值的这类 html 属性是怎么回事,该如何设置值;比如:checked = “checked” vs checked = true

    参考链接:https://stackoverflow.com/questions/10650233/checked-checked-vs-checked-true 问: What is the dif ...

  4. Python学习---Python下[字典]的学习

    Python中唯一的映射类型(哈希表)       -->Java中的HashMap<K,V> Python对key进行了哈希函数运算,根据计算的结果决定value的存储地址,所以字 ...

  5. MEGER sentence in oracle

    MEGE Sentence This oracle tutorial explains how to use the oralce MEGER sentence with syntax and sam ...

  6. yii2.0用gii自动补全代码做的简单增删改查,以及图片上传和展示

    首先已经用gii根据model层生成了控制器,模型,视图层. 表结构为如图所示:表名为zhoukao1,

  7. Windows 10 X64 ISO 专业版&家庭版下载与永久激活

    好久没有更新博客,算算时间,已经有4年了,好吧,今天突发奇想,想把今天安装Windows 10的过程给记录下来. 2015年的时候,微软就发布了Windows 10,当时正版的Win7.Win8都可以 ...

  8. January 26 2017 Week 4 Thursday

    Wasting time is robbing yourself. 浪费时间就是掠夺自己. Wasting time is not only robbing yourself, moreover, i ...

  9. 1874 football game(三分法and method to compute the area of trianngle)

    FInd the max area. 1. 三分法 2. NAN (not comparable with number) http://acm.timus.ru/problem.aspx?space ...

  10. 035server端并发聊天

    import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): # 里面是每个客户端连接执 ...