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.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
随机推荐
- vue ajax返回html代码不渲染解决
<span v-html='lists.html'></span>
- UiAutomator1.0 与 UiAutomator2.0
在使用2.0之前,对android自动化框架也做过一些了解<Android 自动化测试框架>.使用UiAutomator2.0也有一段时间,这里将1.0与2.0进行一个对比总结. Ui ...
- Java的家庭记账本程序(H) :微信小程序 image 标签,在模拟器中无法显示图片?(已解决)
日期:2019.2.25 博客期:036 星期一 吼!今天我还是继续研究了自己的微信小程序,还没有连接数据库,只是在xml的设计上添加了不少东西,大家可以看我的截图,嗯~说到今天的收获,就是 marg ...
- iOS开发多线程之GCD
Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Dispatch Que ...
- DOS批处理高级教程(还不错)(转)
前言 目录 第二节 常用特殊符号 1.@ 命令行回显屏蔽符 2.% 批处理变量引导符 3.> 输出重定向符 4.>> 输出重定向符 ...
- RxJS操作符(一)
一.创建类操作符 创建类操作符是连接传统编程和响应式编程的强梁 from: 可以把数组.Promise.以及Iterable转化为Observable. fromEvent: 可以把事件转化为Obse ...
- 西北地区打不开github的解决办法~
泱泱我大西北,github打不开,的确痛苦的. http://ipaddress.com/ip_lookup/ 先查github.com 可能存在打不开的情况~ 随便找一个web在线代理,就可以查到了 ...
- 清除代码中的svn文件。
@echo on color 2f mode con: cols=80 lines=25 @REM @echo 正在清理SVN文件,请稍候...... @rem 循环删除当前目录及子目录下所有的SVN ...
- vue-cli 构建的项目中 如何使用less
vue-cli 构建的项目默认是不支持 less 的,需要自己添加. 首选,安装 less 和 less-loader ,在项目目录下运行如下命令 npm install less less-load ...
- [CF542A]Place Your Ad Here
[CF542A]Place Your Ad Here 题目大意: 有\(n(n\le2\times10^5)\)个广告和\(m(m\le2\times10^5)\)个电视台,第\(i\)个广告只能在\ ...