pl/sql学习笔记---马士兵教程38-48
Procedure Language/Structure query Language
一、关于语言学习
1、数据类型 2、语法 通过例子来学习很快就能明白
set serverputout on;
begin
dbms_output.put_line(‘HelloWorld!’);
end;
二、结构(declare(可选)、begin、exception(可选)、end)
1、declare例子
declare
v_name varchar2(20);
begin
v_name := 'MyName';
dbms_output.put_line(v_name);
end;
2、exception例子
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;
三、数据类型
1、常用变量类型
(1)、binary_integer:整数,主要用于计数而不是用于表示字段类型。
(2)、number:数字类型
(3)、char 定长字符串
(4)、varchar2:变长字符串
(5)、date:日期
(6)、long:长字符串,最长2GB
(7)、boolean:布尔类型,可以取值:true,false和null值
例子:
declare
v_temp number(1);
v_count binary_integer :=0;
v_sal number(7,2) := 4000.00;
v_date date := sysdate;
v_pi constant number(3,2) := 3.14;
v_valid boolean := false;
v_name varchar2(20) not null := 'Myname';
begin
dbms_output.put_line('v_temp value:' || v_temp);
end;
--变量声明,使用%type属性
例子:
declare
v_empno number(4);
v_empno2 emp.empno%type;
v_empno3 v_empno%type;
begin
dbms_output.put_line('Test');
end;
2、复杂数据类型(table相当于java里的数组,record相当于java中的类)
--Table变量类型
例子
declare
type type_table_emp_empno is table of emp.empno%type index by binary_integer;
v_empnos type_table_empno;
begin
v_empnos(0) := 7369;
v_empnos(2) := 7839;
v_empnos(-1) := 9999;
dbms_output.put_line(v_empnos(-1));
end;
--Record变量类型
declare
type type_record_dept is record
(
deptno dept.deptno%type,
dname dept.dname%type,
loc dept.loc%type
);
v_temp type_record_dept;
begin
v_temp.deptno := 50;
v_temp.dname :='aaaa';
v_temp.loc := 'bj';
dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);
end;
--使用%rowtype(有助于程序维护)
declare
v_temp dept%rowtype;
begin
v_temp.deptno := 50;
v_temp.dname := 'aaaa';
v_temp.loc := 'bj';
dbms_output.put_line(v_temp.deptno || ' ' || v_e_temp.dname);
end;
四、SQL语句的运用
例子:
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
select ename , sal into v_name , v_sal from emp where empno = 7369;
dbms_output.put_line(v_name || ' ' || v_sal);
end;
<注>:sql语句中,select语句必须与into一起用,且返回记录有且仅有一条
declare
v_temp emp%rowtype;
begin
select * into v_emp from emp where empno = 7360;
dbms_output.put_line(v_emp.ename);
end;
declare
v_deptno dept.deptno%type := 50;
v_dname dept.dname %type := 'aaaa';
v_loc dept.loc%type := 'bj';
begin
insert into dept2 values(v_deptno, v_dname, v_loc);
commit;
end;
declare
v_deptno emp2.deptno%type := 10;
v_count number;
begin
--update emp2 set sal = sal/2 wehre deptno = v_deptno;
--select deptno into v_daptno from emp2 where empno = 7369;
select count(*) into v_count from emp2;
dbms_output.put_line(sql%rowcount || '条记录被影响');
commit;
end;
DDL语句
begin
execute immediate 'create table T mmm varchar2(20) default ''aaa'');
end;
3、分支与循环
--if语句
--取出7369的薪水,如果<1200,则输出‘Low’,如果<2000则输出‘Middle’,否则‘High’
declare
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno = 7369;
if(v_sal <1200) then
dbms_output.put_line('Low');
elseif(v_sal<2000) then
dbms_output.put_line('Middle');
else
dbms_output.put_line('High');
end if;
end;
--循环
--do-while循环
declare
i binary_integer := 1;
begin
loop
dbms_output.put_line(i);
i := i+1;
exit when (i>= 11);
end loop;
end;
--while循环
declare
j binary_integer := 1;
begin
while j< 11 loop
dbms_output.put_line(j);
j := j+1;
end loop ;
end;
--for循环
begin
for k in 1..10 loop
dbms_output.put_line(k);
end loop;
end;
--错误处理
declare
v_temp number(4);
begin
select empno into v_temp from emp where deptno = 10;
exception
when too_many_rows then
dbms_output.put_line('太多记录了');
when no_data_found then
dbms_output.put_line('没数据');
when otheres then
dbms_output.put_line('error');
end;
--数据库报错记录 Log
create table errorlog
(
id number primary key,
errcode number,
errmsg varchar2(1024),
errdate date
);
declare
v_deptno dept.deptno%type :=10;
v_errcode number;
v_errmsg varchar2(1024);
begin
delete from dept where deptno = v_deptno;
commit;
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 (java里的iterater) fetch 移动游标
declare
cursor c is select * from emp;
v_emp c%rowtype;
begin
open c;
fetch c into v_emp;
dbms_output.put_line(v_emp.ename);
close c;
end;
--遍历游标的三种方式
(1)、do-while方式
declare
cursor c is select * from emp;
v_emp c%rowtype;
begin
open c;
loop
fetch c into v_empl;
exit when (c%notfound);
dbms_output.put_line(v_emp.ename);
end loop;
close c;
end;
(2)、while方式
declare
curser c is select * from emp;
v_emp emp%rowtype;
begin
open c;
fetch c into v_emp;
while (c%found) loop
dbms_output.put_line(v_emp.ename);
fetch c into v_emp;
end loop;
close c;
end;
(3)、For循环方式
declare
cursor c is select * from emp
begin
for v_emp in c loop
dbms_output.put_line(v_emp.ename);
end loop;
end;
--带参数的游标
declare
cursor c(v_deptno emp.deptno%type, v_job emp.job%type) is
select ename,sal from emp where deptno = v_deptno and job = v_job;
begin
for v_temp in c(30,'CLERK') loop
dbms_output.put_line(v_temp.ename);
end loop;
end;
--可更新的游标
declare
cursor c is
select * from emp2 for update;
begin
for v_temp in c loop
if(v_temp.sal<2000) then
update emp2 set sal =sal*2 where current of c;
elseif(v_temp.sal = 5000) then
delete from emp where current of c;
end if;
end loop;
commit;
end;
pl/sql学习笔记---马士兵教程38-48的更多相关文章
- ORALCE PL/SQL学习笔记
ORALCE PL/SQL学习笔记 详情见自己电脑的备份数据资料
- Oracle之PL/SQL学习笔记
自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了一下,给大家分享,有不足之处还望大家 ...
- PL/SQL学习笔记之数据类型中的标量、LOB
一:标量 标量 即 基本数据类型,主要有4种:数值.字符.布尔类型.日期时间. 1:数值类型 数据类型 描述 PLS_INTEGER 通过2,147,483,647到-2147483648范围内有符号 ...
- [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中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...
随机推荐
- mongoVUE的增删改查操作使用说明(转)
mongoVUE连接数据库 http://jingyan.baidu.com/album/9989c7460fd171f648ecfe06.html?picindex=1 mongoVUE操作数据库 ...
- 黄聪:AngularJS中的$resource使用与Restful资源交互(转)
原文:http://blog.csdn.net/he90227/article/details/50525836 1.AngularJS中的 $resource 这个服务可以创建一个资源对象,我们可以 ...
- 【VSCode】Windows下VSCode编译调试c/c++【更新】
便携版已更新,点此获取便携版 用于cpptools插件的配置文件更新 更新的launch.json // Available variables which can be used inside of ...
- 关于Java大数操作(BigInteger、BigDecimal)
本文目标 可以使用BigInteger操作大整数 可以使用BigDecimal指定小数的保留位数 基础知识 对于二进制来说,最高位代表正负号,-0表示-128,+0表示032位系统int型4个字节:- ...
- java1.8新特性(一)
一直在更新java 版本,原来也没有关注java版本的变化 引入的一些新的api 引起注意的还是 关于一些并发包的使用,那时候才对每个版本的特性 去了解了一下,虽然 不一定都用上了,但是不管学习什 ...
- Composer 安装时要求输入授权用户名密码
composer require "overtrue/laravel-socialite:~2.0" Authentication required (packagist.phpc ...
- sqlserver in 和 exist 子查询
1 in 子查询 use StudentManageDB go --select StudentName from Students --where StudentId=(select Student ...
- Service Worker初体验
http://web.jobbole.com/84792/ http://imweb.io/topic/56592b8a823633e31839fc01
- 虚幻4:2D游戏中实现二级或多级跳跃
转自:http://www.52vr.com/article-729-1.html 闲来无事,想做个二级跳跃或者多级跳跃的方法.. 如下所示.即可实现. 第一步:角色蓝图中.设置跳跃事件 第二部: ...
- java运行原理剖析
java是一种编译型语言,我们开发好的代码是不能够直接运行的,需要我们程序员进行编译之后,将字节点文件载入jvm虚拟之后,才可以运行操作 其原理是 java源代码-------编译-------> ...