PL_SQL学习
打印输出: dbms_output.put_line('AA');
显示服务器输出信息 set serveroutput on;
打印出eid=1的员工姓名:
declare
v_name varchar2(20);
begin
select ename into v_name
from emp
where eid = 1;
dbms_output.put_line(v_name);
end;
/
抛出异常:
declare
v_num number := 0;
begin
v_num := 2/v_num;
dbms_output.put_line(v_num);
exception
when others then
dbms_output.put_line('error');
end;
/
使用%type进行目标类型赋值(若表字段类型改变时变量的类型也会实时更改)
declare
v_eid emp.eid%type;
v_ename emp.ename%type;
v_ename2 v_ename%type;
常用变量类型
1. binary_integer::整数,主要用来计数而不是用来表示字段类型。
2. number:数字类型,可以表示整数和小数。
3. char:定长字符串。
4. varchar2:变长字符串
5. date:日期
6. long:长字符串,最长2GB
7. boolean:布尔类型(true,false,null值)
复合数据类型:
Table变量类型(类比java中的数组)
declare
-- 首先声明名字为type_table_emp_eid类型的数组
type type_table_emp_eid is table of EMP.EID%type index by binary_integer;
v_eids type_table_emp_eid;
begin
v_eids(0) := 12;
v_eids(1) := 11;
v_eids(2) := 2;
v_eids(-1) := 6;
dbms_output.put_line(v_eids(-1));
end;
/
Record变量类型(类比java中的类)
declare
type type_record_emp is record
(
eid emp.eid%type,
ename emp.ename%type,
cid emp.cid%type
);
v_temp type_record_emp;
begin
v_temp.eid := 4;
v_temp.ename := 'rose';
v_temp.cid := 4;
dbms_output.put_line(v_temp.eid || '-' || v_temp.ename);
end;
/
使用%rowtype声明record变量(表结构发生变化时,rocord变量也会实时变化)
接下来是上面代码的优化版:
declare
v_temp emp%rowtype;
begin
v_temp.eid := 4;
v_temp.ename := 'rose';
v_temp.cid := 4;
dbms_output.put_line(v_temp.eid || '-' || v_temp.ename);
end;
/
PL_SQL里的select查询必须和into一块用,而且保证有且只有一条记录
declare
v_ename emp.ename%type;
v_cid emp.cid%type;
begin
select ename,cid into v_ename,v_cid from emp where eid = 1;
dbms_output.put_line(v_ename || '-' ||v_cid);
end;
/
select 与 %rowtype的联合运用
declare
v_emp emp%rowtype;
begin
select * into v_emp from emp where eid = 1;
dbms_output.put_line(v_emp.eid || '-'|| v_emp.ename || '-' || v_emp.cid);
end;
/
insert 的使用,别忘了commit
declare
v_eid emp.eid%type := 4;
v_ename emp.ename%type := 'rose';
v_cid emp.cid%type := 5;
begin
insert into emp values(v_eid,v_ename,v_cid);
commit;
end;
/
update的使用与sql%rowcount(影响记录条数统计)
declare
v_eid emp.eid%type := 2;
v_ename emp.ename%type := 'jeck';
begin
update emp set ename = v_ename where eid = v_eid;
dbms_output.put_line(sql%rowcount || '条记录被影响');
commit;
end;
/
select语句into一条数据给变量,所以影响一行。
declare
v_count number;
begin
select count(0) into v_count
from company;
dbms_output.put_line(sql%rowcount || '条记录被影响');
end;
/
PL_SQL中执行DDL语句时需要添加execute immediate(' xxx ')
begin
execute immediate
'create table test2(
c1 varchar(20)
)';
end;
/
PL_SQL中的条件判断 if .. then .. elsif .. then .. else .. end if
declare
v_cname company.cname%type;
begin
select c.cname into v_cname
from company c
where cid = 3;
if v_cname = '腾讯' then
dbms_output.put_line('qq微信');
elsif v_cname = '百度' then
dbms_output.put_line('搜索引擎');
else
dbms_output.put_line('其他公司');
end if;
end;
/
PL_SQL中的循环
类似java中do...while循环
declare
i binary_integer :=1;
begin
loop
dbms_output.put_line(i);
i := i+1;
exit when(i > 10);
end loop;
end;
/
类似java中while循环
declare
i binary_integer := 1;
begin
while i <= 10 loop
dbms_output.put_line(i);
i := i+1;
end loop;
end;
/
类似java中for循环
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
/
异常处理(实际返回行数大于请求行数)
declare
v_temp number(11);
begin
select cid into v_temp
from company
where cname in ('百度','腾讯');
exception
when too_many_rows then
dbms_output.put_line('太多记录');
when others then
dbms_output.put_line('error');
end;
/
异常处理(没有找到数据)
declare
v_temp number(4);
begin
select cname into v_temp
from company
where cid = 6;
exception
when no_data_found then
dbms_output.put_line('没数据');
end;
/
记录错误信息表以及主键自增序列创建
create table errorlog
(
id number primary key,
errcode number,
errmsg varchar(1024),
errdate date
);
create sequence seq_errorlog_id start with 1 increment by 1;
将删除错误回滚并将出错误信息记录在错误信息表中
declare
v_cid company.cid%type := 1;
v_errcode errorlog.errcode%type;
v_errmsg errorlog.errmsg%type;
begin
delete from company where cid = v_cid;
exception
when others then
rollback;
v_errcode := sqlcode;
v_errmsg := sqlerrm;
insert into errorlog values(seq_errorlog_id.nextval,v_errcode,v_errmsg,sysdate);
commit;
end;
/
游标 CURSOR
CURSOR与“do...while”遍历表中数据
declare
cursor c is
select * from company;
v_company c%rowtype;
begin
open c;
loop
fetch c into v_company;
exit when (c%notfound);
dbms_output.put_line(v_company.cname);
end loop;
close c;
end;
/
CURSOR与“while”遍历表中数据
declare
cursor c is
select * from company;
v_company company%rowtype;
begin
open c;
fetch c into v_company;
while (c%found) loop
dbms_output.put_line(v_company.cname);
fetch c into v_company;
end loop;
close c;
end;
/
CURSOR与“for”遍历表中数据(for循环开始时cursor自动打开,循环结束时cursor自动关闭)
declare
cursor c is
select * from company;
begin
for v_company in c loop
dbms_output.put_line(v_company.cname);
end loop;
end;
/
带参数的游标
declare
cursor c(v_cid company.cid%type, v_cname company.cname%type)
is
select * from company where cid = v_cid and cname = v_cname;
begin
for v_company in c(1,'百度') loop
dbms_output.put_line(v_company.cname);
end loop;
end;
/
可更新游标(一般游标用处是只读,而可更新游标可以更改)
declare
cursor c
is
select * from company for update;
begin
for v_company in c loop
if(v_company.cname = '字节跳动') then
update company set cname = 'gg' where current of c;
end if;
end loop;
commit;
end;
/
存储过程PROCEDURE
简单存储过程创建
create or replace procedure p1
is
cursor c
is
select * from emp;
begin
for v_emp in c loop
if(v_emp.cid = 1) then
dbms_output.put_line('百度1');
elsif(v_emp.cid = 2) then
dbms_output.put_line('阿里巴巴1');
elsif(v_emp.cid = 3) then
dbms_output.put_line('腾讯1');
elsif(v_emp.cid = 4) then
dbms_output.put_line('字节跳动1');
else
dbms_output.put_line('google1');
end if;
end loop;
end;
/
-- 执行存储过程p1
execute p1;
带参数的存储过程创建
create or replace procedure p2(
v_a in number,
v_b in number,
v_max out number,
v_temp in out number
)
is
begin
if(v_a > v_b) then
v_max := v_a;
else
v_max := v_b;
end if;
v_temp := v_temp + 1;
end;
/ -- 执行p2
declare
v_a number := 3;
v_b number := 4;
v_max number;
v_temp number := 5;
begin
p2(v_a, v_b, v_max, v_temp);
dbms_output.put_line(v_max);
dbms_output.put_line(v_temp);
end;
/
函数FUNCTION
create or replace function sal_tax(
v_sal number
)
return number
is
v_rate number;
begin
if(v_sal < 2000) then
v_rate := 0.1;
elsif (v_sal < 3000) then
v_rate := 0.2;
else
v_rate := 0.3;
end if;
return v_sal*v_rate;
end;
/
-- 执行函数sal_tax
select sal_tax(3000) from company;
触发器TRIGGER
-- 用户对员工表的操作日志表
create table emp_log
(
ename varchar2(20),
eaction varchar2(20),
etime date
); -- 创建触发器,将用户对emp表的增删改操作记录插入emp_log表中
create or replace trigger trig
after insert or delete or update on emp (for each row)
begin
if inserting then
insert into emp_log values(user, 'insert', sysdate);
elsif updating then
insert into emp_log values(user, 'update', sysdate);
elsif deleting then
insert into emp_log values(user, 'delete', sysdate);
end if;
end;
/ update emp set cid = 4 where ename = 'rose';
PL_SQL学习的更多相关文章
- 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代
2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Unity3d学习 制作地形
这周学习了如何在unity中制作地形,就是在一个Terrain的对象上盖几座小山,在山底种几棵树,那就讲一下如何完成上述内容. 1.在新键得项目的游戏的Hierarchy目录中新键一个Terrain对 ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- 菜鸟Python学习笔记第一天:关于一些函数库的使用
2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
随机推荐
- Django-DRF-图书增删改查 !!!
自己封装的 class MyResponse(): def __init__(self): self.status = 100 self.msg = None @property def get_ ...
- JavaScript入门学习笔记(一)
W3cJavaScript教程 JS是JavaScript的缩写,而JSP是Java Server Page的缩写,后者是用于服务器的语言. JavaScript代码写在标签<script> ...
- 《剑指offer》第一个只出现一次的字符
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- Windows Internals 笔记——作业
1.Windows提供了一个作业内核对象,它允许我们将进程组合在一起并创建一个“沙箱”来限制进程能够做什么.创建只包含一个进程的作业同样非常有用,因为这样可以对进程施加平时不能施加的限制. 2.如果进 ...
- Navicat for MySQL 连接Mysql8.0 报 1251
先设置Mysql全局 cmd下输入: mysql -uroot -p root密码 use mysql; update user set host = "%" where user ...
- 英语口语练习系列-C40-电器-访友
词汇-电器 dishwasher 洗碗机 fridge 电冰箱 washing machine 洗衣机 mobile phone 手机 digital camera 数码相机 intelligent ...
- 英语口语练习系列-C35-马戏-谈论语言-己亥杂诗
词汇-马戏 circus audience spectator spotlight bandstand magic magician clown spacious attractive product ...
- java实现单链表反转(倒置)
据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; ...
- day10_friest_自动化
一.知识回顾, 1.构造函数:def __del__(self)是类执行完后,需要将某些如连接等关闭,可将关闭代码写在该函数中,既是实例被销毁的时候执行 2.私有寒素:def __say(self)表 ...
- Oracle命令行中显示:ORA-04076: 无效的 NEW 或 OLD 说明
Oracle命令行进行操作时可能出现"ORA-04076: 无效的 NEW 或 OLD 说明" 需要在条件语句中JOB前面添加“old.”即可(因为是在when条件里面,所以不用“ ...