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;
最新文章
- <;十二>;JDBC_批量处理
- VC++ 应用程序无法正常启动0xc0150002
- linux杀死jobs的正确方法
- mysql与sqlserver之间的关系转换
- Html与CSS快速入门02-HTML基础应用
- log4qt的使用
- Python3 配置文件 解析
- Google Protocol Buffers简介
- 跨平台传输中使用base64来保证非ascii码字符串的完整性
- swift 笔记 (十二) —— 下标
- HDU--3829--Cat VS Dog【最大点独立集】
- 关于C#的学习心得体会
- jquery form submit提交后无反应 不报错
- Pipeline build step with parameters
- RT-SA-2019-003 Cisco RV320 Unauthenticated Configuration Export
- linux下gzip的压缩详解
- Xcode搭建Python编译环境
- 好用的模板引擎NVelocity
- 「HNOI2018」转盘
- zabbix_agentd-install.sh (脚本部署zabbix_agentd服务)
热门文章
- 使用JavaScript完成文字向上间歇滚动
- 【ActiveMQ】之安全机制(一)管控台安全设置
- C/C++基础--面向对象编程OOP
- nodejs选择JavaScript作为开发语言,是因为一般的开发语言的标准库都是带有IO模块的,并且通常这个 模块是阻塞性的,所以nodejs选择了没有自带IO模块的Javascript
- [转][C#]单例模式之懒加载
- 廖雪峰Java2面向对象编程-6Java核心类-6常用工具类
- 廖雪峰Java2面向对象编程-6Java核心类-3包装类型
- list函数
- C++ 调用 Lua
- cas server