PL SQL笔记(三)
loop
if credit_rating < 3 then
..
exit;
end if;
end loop;
select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual;
select cast(sysdate as timestamp) from dual;
复合类型数据
1.记录:
declare
type
emp_record_type
is record
(
r_name emp.ename%type,
r_job emp.job%type
);
emp_record emp_record_type;
begin
select t.ename, t.job into emp_record from emp t where t.empno = '';
dbms_output.put_line('ename = ' || emp_record.r_name || ', r_job = ' || emp_record.r_job);
end;
/
2.联合数组:
declare
type
emp_table_type
is table of
emp.ename%type
index by binary_integer;
emp_table emp_table_type;
begin
select ename into emp_table(0) from emp where empno = '';
dbms_output.put_line('ename = ' || emp_table(0));
end;
/
3.嵌套表:
嵌套表和联合数组类似,但嵌套表可以作为列的数据类型使用,而联合数组不能。
create or replace type item_type as object
(
t_username varchar2(20),
t_password varchar2(20),
t_age smallint
);
declare
type itemtable is table of item_type;
v_table itemtable := itemtable();
begin
v_table.extend;
v_table(v_table.last) := item_type('dys', 'dys123', 10);
end;
利用嵌套表当表列数据类型:
create or replace type itemtable is table of Item_Type;
create table TestTable
(
address varchar2(100),
phoneNumber varchar2(11),
itemList itemtable
)
nested table itemList store as itemList;
4.变长数组:
变长数组可以作为表列的数据类型或对象类型属性的数据类型,嵌套表长度没有限制,而变长数组长度有限制:
create or replace type idArray_Type as varray(100) of number; create or replace type item_type as object
(
v_itemCode char(10),
v_name varchar2(20)
); create or replace type itemArray as varray(10) of item_type; create table TestT
(
v_id number(8),
items itemArray
)
pl sql 基本结构:
declare
v_id number(8) := 10;
v_username varchar2(20);
begin
delete from A;
insert into A values(v_id, 'ding', 'ding123');
select username into v_username from A where id = v_id;
dbms_output.put_line('v_username = ' || v_username);
exception
when no_data_found then
dbms_output.put_line('no data');
end;
/
常量:
declare
PI constant number(9) := 3.1415926;
begin
commit;
end;
变量:
declare
age number(3) := 26;
begin
commit;
end;
其他类型:
emp.empno%type
emp%rowtype
分支:
if ... then
if sales > 10 then
compute_bonus(empid);
update payroll set pay = pay + bonus where empno = emp_id;
end if;
if .. then ... else
if trans_type = 'CR' then
update accounts set balance = balance + debit where ...
else
update accounts set balance = balance - debit wehre ...
end if;
if trans_type = 'CR' then
update accounts set balance = balance - debit where ...
else
if new_balance >= minimum_balance then
update accounts set balance = balance - debit where ...
else
raise insufficient_funds;
end if;
end if;
if .. then ...elsif
begin
if sales > 50000 then
bonus := 1500;
elsif sales > 35000 then
bonus := 500;
else
bonus := 100;
end if;
insert into payroll values(emp_id, bonus...);
end;
case语句:
case grade
when 'A' then
dbms_output.put_line('A');
when 'B' then
dbms_output.put_line('B');
else
dbms_output_put_line('wrong!');
end case;
搜寻式case语句:
case
when grade = 'A' then
dbms_output.put_line('A');
when grade = 'B' then
dbms_output.put_line('B');
else
dbms_output.put_line(''wrong!);
end case;
loop
loop
....
end loop;
exit(只能入到循环中,如果普通PL SQL 块要退出用return)
loop
if a > 3 then
...
exit;
end if;
end loop;
exit .. when
loop
fetch c1 into ...
exit when c1%notfound;
...
end loop;
close c1;
if == exit ... when
if a > 100 then
exit;
end if; -----------------------------------------------------------
exit when a > 100;
loop label(循环标签)
<<outer>>
loop
...
loop
...
exit outer when ...
end loop;
end loop outer;
while ... loop
while a < 100 loop
...
select sal into salary from emp where x = x;
...
total := total + salary;
end loop;
其他用法:
loop
...
exit when a > 10;
end loop;
--------------------------------------------
do{
} while()
---------------------------------------------
done := false;
while not done loop
....
done := boolean_expression;
end loop;
for ... loop
declare
type datelist is table of date index by binary_integer;
dates datelist;
k constant integer := 5;
begin
for j in 1 .. 3 loop
dates(j * k) := sysdate;
end loop;
end;
select count(empno) into emp_count from emp;
for i in 1 .. emp_count loop
...
end loop;
-----------------------------------------------------------
<<main>>
declare
ctr integer;
begin
...
for ctr in 1 .. 25 loop
...
if main.ctr > 10 then
...
end if;
end loop;
end main;
for exit
for j in 1 .. 10 loop
fetch cl into emp_rec;
exit when cl%notfound;
...
end loop;
-------------------------------------------------
<<outer>>
for i in 1 .. 5 loop
...
for j in 1 .. 10 loop
fetch cl into emp_rec;
exit outer when cl%notfound;
...
end loop;
end loop outer;
goto
declare
done boolean;
for i in 1 .. 10 loop
if done then
goto end_loop;
end if;
...
<<end_loop>>
null;
end loop;
endl;
declare
my_ename char(10);
begin
<<get_name>>
select ename into my_ename from emp wher ...
begin
...
goto get_name;
end;
end;
null
exception
when zero_divide then
rollback;
when value_error then
insert into errors values...
when others then
null;
if rating > 90 then
compute_bonus(emp_id);
else
null
end if;
DCL(数据控制语句)
| 权限 | 说明 |
| create user | 创建其他用户(dba角色) |
| drop user | 删除其他用户 |
| select any table | 查询任何用户表或视图 |
| create any table | 在任何表空间中创建表 |
| drop any table | 删除在任何表空间中所创建的表 |
| create session | 连接数据库 |
| create table | 在用户自己表空间中创建表 |
| create view | 在用户自己表空间中创建视图 |
| create sequence | 在用户自己的表空间中创建序列 |
| create proceudre | 在用业内自己表空间中创建存储过程 |
授权:
grant create any table to scott;
撤销授权:
revoke create any table from scott;
保存点:
savepoint a;
execute dbms_transaction.savepoint('B');
回滚保存点:
rollback to B;
exeucte dbms_transaction.rollback_savepoint('A');
回滚全部事物:
rollback;
execute dbms_transaction.rollback;
PL SQL笔记(三)的更多相关文章
- pl/sql 笔记之基础(上)
由于公司中使用 oracle,而本人对存储过程一直也懵懵懂懂,故一周时间学习了一遍 pl/sql,在此记下笔记!!! 一.前提,pl/sql 是啥? 1.PL/SQL是一种高级数据库程序设计语言,该语 ...
- pl/sql 笔记之存储过程、函数、包、触发器(下)
一.存储过程.存储函数 1.What's This? ①.ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数. ②.存储过程.存储函数的唯 ...
- 二十三、oracle pl/sql分类三 包
包用于在逻辑上组合过程和函数,它由包规范和包体两部分组成.1).我们可以使用create package命令来创建包,如:i.创建一个包sp_packageii.声明该包有一个过程update_sal ...
- PL/SQL第三章 基础查询语句
--查询所有列 select * from tab_name|view_name; SELECT * FROM emp; SELECT * FROM (SELECT * FROM emp); --查询 ...
- PL/SQL笔记(一)
PL/SQL概述 PL/SQL是一种高级的数据库程序设计语言,专门使用与Oracle语言基于数据库的服务器的内部,所以PL/SQL代码可以对数据库进行快速的处理. 1.什么是PL/SQL? PL/SQ ...
- PL/SQL笔记(1)-流程控制,循环,异常,块
流程控制 1.If,then,else,elsif(不是elseif) ' then null; endif; 2.Case 简单case表达式: 搜索型Case表达式: 3.goto语句 begin ...
- Oracle学习笔记十 使用PL/SQL
PL/SQL 简介 PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的编程语言,是对 SQL 的扩展,它支持多种数据类型,如大对象和集合类型,可使用 ...
- Oracle PL/SQL语句基础学习笔记(上)
PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE server和其它工具中了,近几年中很多其它的开发者和DBA開始使用PL/SQL,本文将讲述PL ...
- Oracle PL/SQL
PL/SQL 简介 PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的编程语言,是对 SQL 的扩展,它支持多种数据类型,如大对象和集合类型,可使用 ...
随机推荐
- 剑指offer-第二章算法之斐波拉契数列(青蛙跳台阶)
递归与循环 递归:在一个函数的内部调用这个函数. 本质:把一个问题分解为两个,或者多个小问题(多个小问题相互重叠的部分,会存在重复的计算) 优点:简洁,易于实现. 缺点:时间和空间消耗严重,如果递归调 ...
- Linux/Unix shell 监控Oracle告警日志(monitor alter log file)
使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等.本文给出Linu ...
- php-LAMP试题
ylbtech-doc:php-LAMP试题 LAMP试题 1.A,LAMP试题返回顶部 1.{PHP LAMP题目}变量$email的值是字符串 user@example.com ,以下哪项能把字符 ...
- <转>安卓应用测试checklist
启动: 1. 启动入口:桌面正常启动,最近运行启动,所有程序列表中启动,锁屏快捷启动 2. 其他入口:从其他程序开启应用,从外部以文件形式打开应用(如果有) 3. 退回:从其他程序退回时回到被测应用, ...
- Cocos2d-android (01) 创建一个简单的cocos2d应用程序
下载Cocos2d-android的源代码:cocos2d-android-1 git@github.com:ZhouWeikuan/cocos2d.git 将项目导入到eclipse中.运行实例: ...
- Real Boxing 2
http://itunes.apple.com/ca/app/real-boxing-2/id932779605?mt=8&ign-mpt=uo%3D4
- linux 为开机菜单加密码·
首先是在/boot/grub/menu.lst 里面添加密码的,但是需要是加密过后的,否则人家直接跑到你的menu.lst里面查看密码不就行了.... 于是,可以使用grub提供的md5加密功能: # ...
- linux下安装filezilla客户端遇到的问题
访问filezilla ./filezilla 出现error while loading shared libraries : libpng12.so.o 缺少libpng12.so.o这个文件 解 ...
- NServiceBus-日志
默认的日志 NServiceBus一些有限,固执己见,内置的日志记录. 默认的日��行为如下: 控制台 所有 Info(及以上)消息将被输送到当前的控制台. 错误将会写 ConsoleColor.Re ...
- 《学习OpenCV》练习题第四章第一题b&c
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...