Pl/sql学习笔记2
--
declare
type vsal_table is
table
of emp.sal%type;
a vsal_table;
begin
--必须得初始化 并且有数量上的区分 从一开的
a:=vsal_table(1000,2000,30000);
dbms_output.put_line(a(1));
end;
--使用嵌套表的案例
--定义一个类型
create
type stu_name is
table
of
varchar2(20);
create
table stu (
sid
number(9),age number(2)
, sname stu_name
)nested
table sname store
as stu_name_table;
select
*from stu;
--此时嵌套表就已经建立好了 需要插入语句
insert
into stu values
(1,20,stu_name('cheng','zhi'));
--查询sname 然后遍历出来
declare
sname stu_name;
begin
select sname into sname from stu where
sid=1;
for i in sname.first..sname.last loop
dbms_output.put_line(sname(i));
end
loop;
end;
--更新数据
declare
sname1 stu_name:=stu_name('zhang','san');
begin
update stu set sname=sname1 where
sid=1;
end;
--变长数组 array 需要预先指定一种长度 并进行赋值
declare
type v_ename is
array(3)
of emp.ename%type;
v1 v_ename:=v_ename('aa','bb','cc');
begin
select ename into v1(2)
from emp where empno=7369;
dbms_output.put_line(v1(2));
end;
--常用的函数
--collectionname.method_name(arg);
--exists是否存在什么样的元素 必须传入一个number类型的数据
--count 是取集合的长度 元素的个数
--limit 返回集合的最大个数
--prior 取出当前的上一个元素
--next 下一个的下标
declare
type v_ename is
array(20)
of emp.ename%type;
v1 v_ename:=v_ename('aa','bb','cc');
begin
if v1.exists(1)
then
dbms_output.put_line(v1.count);
dbms_output.put_line(v1.limit);
dbms_output.put_line('hava');
dbms_output.put_line(v1(2));
dbms_output.put_line(v1(v1.prior(2)));
dbms_output.put_line(v1(v1.next(2)));
else
dbms_output.put_line('no');
end
if;
end;
--delete函数
declare
type vsal_table is
table
of emp.sal%type;
a vsal_table;
begin
--必须得初始化 并且有数量上的区分 从一开的
a:=vsal_table(1000,2000,30000);
dbms_output.put_line(a(1));
dbms_output.put_line(a.count);
a.delete(3);
dbms_output.put_line(a.count);
end;
--删除的时候下标不会发生变化 位置不会变化
--oracle 里面的批量处理的问题
create
table t1(tid number(9),tname varchar2(9));
select
*
from t1;
declare
type id_table is
table
of
number(9)
index
by
binary_integer;
type name_table is
table
of
varchar2(9)
index
by
binary_integer;
start_time number(10);
end_time number(10);
v_id_table id_table;
v_name_table name_table;
begin
for i in
1..10000 loop
v_id_table(i):=i;
v_name_table(i):=to_char(i);
end
loop;
start_time:=dbms_utility.get_time;
for i in
1..v_id_table.count loop
insert
into t1 values(v_id_table(i),v_name_table(i));
end
loop;
end_time:=dbms_utility.get_time;
dbms_output.put_line(end_time-start_time);
end;
--瞬间插入10000条记录
create
table t2(tid number(9),tname varchar2(9));
select
count(*)
from t2;
declare
type id_table is
table
of
number(9)
index
by
binary_integer;
type name_table is
table
of
varchar2(9)
index
by
binary_integer;
start_time number(10);
end_time number(10);
v_id_table id_table;
v_name_table name_table;
begin
for i in
1..10000 loop
v_id_table(i):=i;
v_name_table(i):=to_char(i);
end
loop;
start_time:=dbms_utility.get_time;
forall i in
1..v_id_table.count
insert
into t2 values(v_id_table(i),v_name_table(i));
end_time:=dbms_utility.get_time;
dbms_output.put_line(end_time-start_time);
end;
--批量处理=之更新
declare
type id_table is
table
of
number(9)
index
by
binary_integer;
type name_table is
table
of
varchar2(9)
index
by
binary_integer;
vid id_table;
vname name_table;
begin
for i in
1..100 loop
vid(i):=i;
vname(i):=to_char(i)||'name';
end
loop;
forall i in
1..vid.count
update t2 set tname=vname(i)
where tid=vid(i);
end;
select
*
from t2 where tname='1name';
--使用 bulk collect 处理批量任务
declare
type t2_table is
table
of t2%rowtype
index
by
binary_integer;
vt t2_table;
begin
select
*
bulk
collect
into vt from t2;
end;
--游标案例
declare
v_emp emp%rowtype;
--声明游标
cursor cur_emp is
select
*
from emp;
begin
--open cursor
open cur_emp;
loop
--fetch
fetch cur_emp into v_emp;
exit
when cur_emp%notfound;
dbms_output.put_line(v_emp.ename||' '||v_emp.sal);
end
loop;
--close cursor
close cur_emp;
end;
--灵活掌握
declare
type r1 is
record
(
ename emp.ename%type,
sal emp.sal%type
);
type a1 is
table
of r1 index
by
binary_integer;
a a1;
begin
select ename ,sal bulk
collect
into a from emp;
for i in a.first..a.last loop
dbms_output.put_line(a(i).ename||' '||a(i).sal);
end
loop;
end;
Pl/sql学习笔记2的更多相关文章
- ORALCE PL/SQL学习笔记
ORALCE PL/SQL学习笔记 详情见自己电脑的备份数据资料
- Oracle之PL/SQL学习笔记
自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了一下,给大家分享,有不足之处还望大家 ...
- [Oracle] PL/SQL学习笔记
-- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...
- PL\SQL学习笔记
注释 单行--多行 一.declare一般用于做变量的申明.begin 程序体开始执行 end; 程序体结束exception .. dbms_output.put_line('绝对值'||v_ab ...
- PL/SQL学习笔记_01_基础
PL/SQL语句可以在Oracle客户端的 SQL窗口或者 command 窗口中运行 在SQL窗口中运行步骤同 SQL语句 在command 窗口中运行的步骤如下: 1)File—new com ...
- PL/SQL学习笔记程序单元
一:程序单元组成 一个PL/SQL程序单元主要包括三部分: 声明与定义部分:声明变量.常量.类型等:定义过程.函数等: 执行部分:执行PL/SQL语句:调用过程.参数:处理游标等: 异常处理部分:处理 ...
- PL/SQL学习笔记之日期时间
一:PL/SQL时间相关类型 PL/SQL提供两个和日期时间相关的数据类型: 日期时间(Datetime)数据类型 时间间隔类型 二:日期时间类型 datetime数据类型有: DATE TIMEST ...
- PL/SQL学习笔记之包
一:包 包是由一组相关的函数,过程,变量,游标等PL/SQL程序设计元素的组合而成的一个PL/SQL程序单元,相当于Java中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...
- PL/SQL学习笔记之集合
一:PL/SQL集合 集合是一个有序且存有相同的类型数据的数据结构. PL/SQL提供了三种集合类型: 索引表(关联数组) 嵌套表 数组 二:索引表:一个索引表(也叫关联数组)是一组键 - 值对.每个 ...
- PL/SQL学习笔记之异常
一:异常 程序执行过程中出现错误情况被称为异常,主要有两种类型的异常: 系统定义的异常 用户定义的异常 二:系统定义的异常 Exception Oracle Error SQLCODE 描述 ACCE ...
随机推荐
- 纯CSS + 媒体查询实现网页导航特效
纯css+媒体查询实现网页导航特效 附上效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html lang="en"> <hea ...
- Mysql数据库 day1
Mysql数据库属于关系型数据库(mysql.oracle.sql server),非关系型数据库有DB2.Redis MySQL执行原理,逻辑分层.更改数据库处理引擎 作者:Stanley 罗昊 [ ...
- Linux服务-mysql基础篇
目录 1. 关系型数据库介绍 1.1 数据结构模型 1.2 RDBMS专业名词 1.3 关系型数据库的常见组件 1.4 SQL语句 2. mysql安装与配置 2.1 mysql安装 2.2 mysq ...
- Spark MemoryManager内存模型
- 关于Quartus+Modelsim 门级仿真 Warning (vopt-2216) Cannot find instance 'NA' specified in sdf.的解决办法
本文操作环境:Win 7 32位系统, Quartus II 11.1 ,Modelsim SE 10.1a 在Quartus II中调用Modelsim SE做Gate Level Simulait ...
- 编写Dockerfiles
指令 docker build通过Dockerfile制作镜像 docker build [PATH] [-f Dockerfile] 其中PATH不写,默认执行指令的当前目录,不要用 /,这样将导致 ...
- Velocity的学习1
Velocity 是一个基于Java的的模板引擎,通过特定的语法,速度可以获取在的Java语言中定义的对象,从而实现界面和Java的代码的真正分离,这意味着可以使用Velocity替代JSP的开发模式 ...
- 洛咕 P2468 [SDOI2010]粟粟的书架
强行二合一啊... 前面直接二分最小值,二维前缀和.后面用主席树查最小值.注意要写\(nlogn\). // luogu-judger-enable-o2 #include<bits/stdc+ ...
- Object C学习笔记3-对象的使用和定义
1. 如何定义一个对象 在面向对象的语言中,定义一个对象是使用Class关键字,而在Object-C中则是使用@interface,@interface用于定义对象的属性和方法,@implementa ...
- Navigation - How to define the structure of the navigation tree via the NavigationItemAttribute
In the meantime, you should use the Model Editor to create such a navigation structure. There are se ...