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 ...
随机推荐
- boost::bind 学习
最近学习了太多与MacOS与Iphone相关的东西,因为不会有太多人有兴趣,学习的平台又是MacOS,不太喜欢MacOS下的输入法,所以写下来的东西少了很多. 等我学习的东西慢慢的与平台无关的时 ...
- x$ksppi与x$ksppcv查询隐藏参数
数据库版本:oracle11g 11.0.2.0.4 SQL> desc x$ksppi; Name Null? Type -------------------- -------- --- ...
- 在hue平台上使用oozie工作流调度
在实习期间,公司使用的hue平台做的数仓,下面就简单介绍一下hue的一些使用的注意事项,主要是工作流的使用和调度 进入hue首页: Workflow是工作流,Schedule是调度工作流的,如设置工作 ...
- java rmi 入门实例
java rmi 入门实例 (2009-06-16 16:07:55) 转载▼ 标签: java rmi 杂谈 分类: java-基础 java rmi即java远程接口调用,实现了2台虚拟机之 ...
- 2017-2018-1 20155230 mypwd实现
mypwd实现 每个文件夹都有以".",".."为名的文件.首先记录"."所拥有的i-节点号,然后使用chdir向上进入父目录查找含有刚记录 ...
- 2015306 白皎 《网络攻防》Exp4 恶意代码分析
2015306 白皎 <网络攻防>Exp4 恶意代码分析 netstat [Mac.Linux.Win] sysinteral [MS]:1 2 3 一.系统监控--Windows计划任务 ...
- Node.js 从入门到茫然系列——入门篇
在创建服务的时候,我们一般代码就是: var http = require("http"); var server = http.createServer(function(req ...
- Codeforces VK Cup 2018 Div.2
总题面传送门 这次考试只过了3题,前三题题目难度并不大,但是第三题的代码细节卡了我两个半小时(基本上整场考试),所以以后要合理把握时间,注意把握代码细节,并更加完善我的代码风格,使其更加简练.(赛外话 ...
- [2016北京集训试题14]股神小D-[LCT]
Description Solution 将(u,v,l,r)换为(1,u,v,l)和(2,u,v,r).进行排序(第4个数为第一关键字,第1个数为第二关键字).用LCT维护联通块的合并和断开.(维护 ...
- 【LG3246】[HNOI2016]序列
[LG3246][HNOI2016]序列 题面 洛谷 题解 60pts 对于每个位置\(i\),单调栈维护它往左第一个小于等于它的位置\(lp_i\)以及往右第一个小于它的位置\(rp_i\). 那么 ...