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.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
随机推荐
- C#+EntityFramework编程方式详细之Database First
Database First “Database First”模式即“数据库优先”,其实Database First 与Model First 很类似,只不过一个是有数据可一个是创建数据库,具体的操作 ...
- 【异常处理】Java异常如何做异常处理
类似SpringMVC项目的异常处理可以这样做: 整个项目创建全局的: 1.一个自定义异常如OneException和错误码,统一封装所有异常. 2.一个返回实体类ResponseEntity,包含返 ...
- 《剑指offer》丑数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- redis-string操作
操作之String操作 String操作,redis中的String在在内存中按照一个name对应一个value来存储.如图: set(name, value,ex=None,px=None,nx=F ...
- php实现微信企业向用户付款
<?php header('content-type:text/html;charset=utf-8'); $data['mch_appid']='##################';//商 ...
- teamviewer连接未就绪的解决(Manjaro Linux)
放假回家,想设置一下teamviewer,结果一直报错"TeamViewer not ready. Please check your connection" 查了一通,发现这个方 ...
- 【软件工程】分布式版本控制系统Git的安装与使用
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远端库地址:https://github.com/Richa ...
- websocket-heartbeat-js心跳检测库正式发布
前言: 两年前写了一篇websocket心跳的博客——初探和实现websocket心跳重连. 阅读量一直比较大,加上最近考虑写一个自己的npm包,因此就完成了一个websocket心跳的检测库.在这 ...
- Windows应用程序组成及编程步骤
Windows应用程序组成及编程步骤: 1.应用程序的组成:一个完整的Windows应用程序通常由五种类型的文件组成 1.C语言源程序文件 2.头文件 3.模块定义文件 4.资源描述文件 5.项目文件 ...
- BigDecimal 准确的 double , float 计算
public class ArithUtil { private static final int DEF_DIV_SCALE = 10; private ArithUtil() { } /** * ...