过程,函数,触发器是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. 爬虫之lxml - etree - xpath的使用

    # 解析原理: # - 获取页面源码数据 # - 实例化一个etree对象,并且将页面源码数据加载到该对象中 # - 调用该对象的xpath方法进行指定标签定位 # - xpath函数必须结合着xpa ...

  2. eclipse插件开发常见的问题及解决办法

    莫名其妙地我的某个Plug-in Projects出现了这样的Error:An API baseline has not been set for the current workspace.虽然后来 ...

  3. HTML 5篇(持续更新)

    1.sessionStorage .localStorage 和 cookie 之间的区别 (一)共同点:都是保存在浏览器端,且同源的. (二)区别:cookie数据始终在同源的http请求中携带(即 ...

  4. gradle中文学习资料

    http://wiki.jikexueyuan.com/project/GradleUserGuide-Wiki/ https://www.gitbook.com/book/lippiouyang/g ...

  5. Php 性能参数优化 及 Iptables 防火墙限制用户访问平率

    Php-Fpm.Conf 文件配置优化 [global] pid = run/php-fpm.pid process_control_timeout=5 [www] listen.allowed_cl ...

  6. Linux->解决用userdel删除不掉用户的问题

    情况: 一般我们移除,都是先把用户从组中删除,再依次把组删掉,但是这里出现了问题: root@ per# userdel -r mysql userdel: user mysql is current ...

  7. .net Basic

    Java's concurrent API https://code.google.com/p/netconcurrent/ java 之DelayQueue实际运用示例 阻塞任务队列DelayQue ...

  8. SAP CRM One Order跟踪和日志工具CRMD_TRACE_SET

    事务码CRMD_TRACE_SET激活跟踪模式: 在跟踪模式下运行One Order场景.运行完毕后,使用事务码CRMD_TRACE_EVAL: 双击参数,就能看到参数明细: 点Callstack也能 ...

  9. API 网关

    使用 API 网关   链接:https://github.com/oopsguy/microservices-from-design-to-deployment-chinese译者:Oopsguy ...

  10. 连接IBM MQ原因码报2035的错误解决办法

    我们的系统使用了ibm mq,用户用来向国家局上报文件和接收文件,前几天用户说上报一直不成功.由于 开发这块程序的人已经辞职了,我觉定在我的机器部署一套,研究一下.我的思路: 在我的机器上安装mq,建 ...