PLSQL 是 Oracle 对 sql 语言的过程化扩展, 指在 SQL 命令语言中增加了过程处理语句(如分支、循环等), 使 SQL 语言具有过程处理能力。 把 SQL 语言的数据操纵能力与过程语言的数据处理能力结合起来, 使得 PLSQL 面向过程但比过程语言简单、高效、灵活和实用。 [declare] –声明变量 begin – 程序体 [exception] –例外;异常 end; –declare和exception部分可以没有,但是begin部分和end必须要有。 –定义变量 declare –变量的赋值 –方式一:使用:=进行赋值; –普通的变量 i number(10) := 1; begin –输出i的值到控制台中 dbms_output.put_line(‘i的值:’||i); – 对i加1 输出 i := i+1; dbms_output.put_line(‘i+1的值:’||i); end; –定义变量 declare –记录型变量:行类型 pemp emp%rowtype; begin –给行类型的变量赋值: 使用关键字 into –方式二:使用select…into子句进行赋值; select * into pemp from emp where empno=7369; –输出行类型变量到控制台中 dbms_output.put_line(pemp.ename||’=============’||pemp.job); end; –使用select…into子句进行赋值时,变量的数据类型要与表中字段的数据类型一致. –变量的特殊类型: –%rowtype:指定该变量与某个表的结构相同。 –该类型的变量可以用来保存表的一行数据; –定义变量 declare –引用型变量:引用某张表的某列的类型 pname emp.ename%type; –pname varchar2(1); --普通 begin –赋值:给引用型变量赋值 select ename into pname from emp where empno=7788; –输出 dbms_output.put_line(pname); end;
–变量的特殊类型: –%type:指定该变量的类型与表的某列的数据类型相同;
运算符 – := 赋值运算符 –普通的变量 i number(10) := 1;
– … 范围运算符
–我插入100万条数据到person表 create table person( id number(10) primary key, name varchar2(200), gender number(1) default 1 );
–plsql去插入一百万条数据到person表中 –介绍oracle的uuid的方法 select sys_guid() from dual;
–创建序列 create sequence seq_test;
–添加100W条记录 declare pname varchar2(200); begin for i in 1…1000000 loop select sys_guid() into pname from dual; insert into person values(seq_test.nextval,pname,1); end loop; commit; end; 流程控制 if 条件 then …. elsif 条件2 then … … else
end if; –在控制台输入1,在控制台打印:我是一 declare –定义变量 i number(10) := &val; begin if i=1 then dbms_output.put_line(‘我是一’); end if; end;
declare –定义变量 i number(10) := &val; begin if i=1 then dbms_output.put_line(‘我是1’);
elsif i > 1 and i<10 then dbms_output.put_line('我大于1小于10'); else dbms_output.put_line('我是'||i); end if;end;
while循环: while 条件 loop –循环体 end loop;
loop循环: loop exit when 退出条件; –循环体 end loop;
for循环: for 变量 in [reverse] 开始值…结束值 loop –循环体 end loop; –&val 即为控制台输入 –求1到&val的和 declare i number(10) :=1; val number(10):=&val; sums number(10):=0;
begin while i<=val loop sums:= sums+i; i :=i+1; end loop;
dbms_output.put_line(sums);
end;
declare i number(10):=1; val number(10):=&val; sums number(10):=0; begin loop exit when i>val; sums:=sums+i; i:=i+1; end loop; dbms_output.put_line(sums); end;
declare val number(10):=&val; sums number(10):=0; begin for i in 1…val loop sums:=sums+i; end loop; dbms_output.put_line(sums);
end; 异常 Oracle异常分为两种:自带异常、自定义异常。
–捕获异常的命令格式: exception when 异常名1 then … when 异常名2 then … … when others then …
系统定义异常 no_data_found (没有找到数据) too_many_rows (select …into 语句匹配多个行) zero_divide ( 被零除) value_error (算术或转换错误) timeout_on_resource (在等待资源时发生超时 declare i number(10); –行类型 pemp emp%rowtype; begin –给i赋值 –i := 1/0; –i := ‘abc’;
–模拟异常:找多条数据给行类型赋值会出现异常 select * into pemp from emp;
exception when zero_divide then dbms_output.put_line(‘分母不能为零’); when value_error then dbms_output.put_line(‘类型转换异常’); when too_many_rows then dbms_output.put_line(‘期望一条数据,但是返回多条’); when others then dbms_output.put_line(‘亲,有异常要处理’); end; 自定义异常 语法格式: 异常名 exception;
抛出异常的方式: 方式一:使用raise关键字; 格式: raise 自定义异常名
方式二:使用raise_application_error(code, message)抛出异常; code:异常的编号,取值范围:-20999~-20000之间任意一个数字; message:异常的信息;
如果要捕获raise_application_error抛出的异常,还需要把异常编号与一个异常的变量进行绑定。 pragma exception_init(异常变量, 异常编号); –自定义异常 declare –定义异常 feifeiexception exception; –定义变量 i number := 1; begin if i=1 then – 抛出一个自定义的异常 raise feifeiexception; end if; exception when feifeiexception then dbms_output.put_line(‘自定义异常出现了…’); end;