1.常量
定义语法格式:
常量名 constant 类型标识符 [not null]:=值;
如:PI constant number(9):=3.1415;
2.基本数据类型变量
基本数据类型
number 数字型
int 整数型
pls_integer 整数型,产生溢出时出现错误
binary_integer 整数型,表示带符号的整数
char 定长字符型,最大255个字符
varchar2 变长字符型,最大2000个字符
long 变长字符型,最大2GB
date 日期型
boolean 布尔型(true,false,null三者之一)
基本数据类型变量的定义语法格式
变量名 类型标识符 [not null]:=值;
如:myvar varchar2(10):='right';
3.复合数据类型变量
3.1 表字段类型变量(使用%type定义)
变量的类型与数据表中的字段的数据类型一致。当数据库表的字段类型修改后,相应的变量的类型也自动修改。
定义语法格式:变量名 表字段名%type;
如:mydate tempuser.testtable.currentdate%type 定义了名为mydate的变量,其类型与tempuser.testtable表中的currentdate字段类型一致。
3.2 记录类型变量
记录类型定义语法格式:
type 数据类型名 is record(
字段1 类型标识符1,
字段2 类型标识符2,
......
1.条件控制
1.1 if .. then .. end if
if 条件 then
语句段;
end if;
1.2 if .. then .. else .. end if
if 条件 then
语句段;
else
语句段;
end if;
1.3 if嵌套
2.循环控制
2.1 loop .. exit .. end loop
loop
循环语句段;
if 条件语句 then
语句段;
exit;
else
语句段;
end if
end loop;
2.2 loop .. exit .. when .. end loop
loop
语句段;
exit when 条件语句;
end loop;
2.3 while .. loop .. end loop
while 条件 loop
语句段;
end loop;
2.4 for .. in .. loop.. end
for 循环变量 in [reverse] 循环下界..循环上界 loop
语句段;
end loop;
注意:reverse表示从后往前循环,循环上下界不变
示例:
set serveroutput on
declare
i integer:=0;
begin
for i in ......
事务是Oracle9i中进行数据库操作的基本单位,在PL/SQL程序中,可以使用3个事务处理控制命令。
1. commit命令
commit是事务提交命令。Oracle9i数据库中,为保证数据一致性,在内存中为每个客户机建立工作区,客户机对数据库进行操作的事务都在工作区完成,只有执行commit命令后,工作区内的修改内容才写入到数据库上,称为物理写入。
可以通过下列命令开启和关闭自动提交功能:
set auto on|off;
2.rollback命令
rollback是事务回滚命令。
3.savepoint命令
savepoint是保存点命令。事务通常由数条命令组成,可以将每个事务分成若干个部分进行保存,这样每次可以回滚每个保存点,而不必回滚整个事务。语法格式如下
创建保存点:savepoint 保存点名;
回滚保存点:rollback to 保存点名;
......
游标是从数据库中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向首记录, 利用fetch语句移动该指针,从而对游标中的数据进行各种操作。
1.定义游标
cursor 游标名 is select语句;
2.打开游标
open 游标名;
3.提取游标数据
fetch 游标名 into 变量名1, 变量名2, ....;
或
fetch 游标名 into 记录型变量名;
4.关闭游标
close 游标名;
5.游标的属性
%isopen
该属性标识游标是否打开,如果没有打开游标就使用fetch语句将提示错误。
%isfound
该属性标识一个fetch语句是否有值,有值返回true,否则返回false。
%notfound
该属性与%isfound相反。
%rowcount
该属性用于获取游标的数据行数。
示例:
set serveroutput on
declare
tempsal scott.emp.sal%type;
cursor mycursor is
select * from scott.emp
where sal>tempsal;
cursorrecord mycursor%rowtype;
begin
tempsal:=800;
open mycursor;
if mycursor%isopen then
fetch mycursor into cursorrecord;
& ......
1.创建过程
create or replace procedure 过程名 as
声明语句段;
begin
执行语句段;
exception
异常处理语句段;
end;
2. 带参数的过程
参数类型3种
in参数:读入参数,主程序向过程传递参数值
out参数:输出参数,过程向主程序传递参数值
in out参数:双向参数
定义带参数的过程示例:
Set serveroutput on
create or replace procedure scott.tempprocedure(
tempdeptno in scott.dept.deptno%type, --输入参数
tempdname out scott.dept.dname%type, --输出参数
temploc in out scott.dept.loc%type) as -- 双向参数
loc1 scott.dept.loc%type;
dname1 scott.dept.dname%type;
begin
select loc into loc1
from scott.dept
where deptno=tempdeptno;
select dname into dname1
from scott.dept
where deptno=tempdeptno;
temploc:='地址:'||loc1;
tempdname:='姓名 ......
Oracle9i异常处理分为系统预定义异常处理和自定义异常处理两部分。
自定义异常处理
1.定义异常处理
declare 异常名 exception;
2.触发异常处理
raise 异常名
3.处理异常
exception
when 异常名1 then
异常处理语句段1;
when 异常名2 then
异常处理语句段2;
示例:
set serveroutput on
declare
salaryerror exception;
tempsal scott.emp.sal%type;
begin
select sal into tempsal
from scott.emp
where empno=7566;
if tempsal<900 or tempsal>2600 then
raise salaryerror;
end if;
exception
when salaryerror then
dbms_output.put_line('薪水超出范围');
end;
......