过程,函数,触发器是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

最新文章

  1. .NET SDK和下载
  2. Yii中CDbCriteria常用方法
  3. yii2源码学习笔记
  4. Stata和Matlab联合处理金融数据
  5. wefwewewe
  6. spdlog源码阅读 (1): sinks
  7. E. Fish (概率DP)
  8. No module named zope.interface error的解决
  9. LitepalNewDemo【开源数据库ORM框架-LitePal2.0.0版本的使用】
  10. 2D-2D:对极几何 基础矩阵F 本质矩阵E 单应矩阵H
  11. [hashcat]基于字典和暴力破解尝试找到rar3-hp的压缩包密码
  12. U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
  13. [Android][Framework] 添加系统服务
  14. Git学习笔记——搭建远程仓库
  15. python 生成器的GeneratorExit异常
  16. wdcp环境安装filephp扩展
  17. ispoweroftwo 判断2的次幂【转】
  18. FATAL ERROR: Tried to use mysqladmin in group......
  19. 03.CSS动画-->自定义动画
  20. Java StringJoiner

热门文章

  1. Web前端面试指导(十三):css样式的优先级是怎么样的?
  2. js Array数组对象常见方法总结
  3. 小小的js
  4. Jave 之方法-函数(5)
  5. C# textBox限定输入数字
  6. Django_MTV模型
  7. androidtab
  8. Installing TensorFlow on Ubuntu
  9. IIS7下设置上传大小的限制
  10. Centos7开放端口