Pl/sql程序中,定义变量,常量:
类型:
1, 标量类型:scalar:
2, 复合类型:composite
3, 参照类型: reference
4, Lob(图像声音类型,大数据)
标量:
V_ename varchar2(10);
V_sal number(7,2);
V_sl2 number(7,2):=5.4;注意赋值是:=
V_hiredate date;
V_valid Boolean not null default false: 定义的一个初始值为false的不能为空的布尔变量
复合变量:
用于存放多个值的变量。
在pl/sql中主要有4中复合变量:
1, 记录
2, 表
3, 嵌套表
4, 变长数组Varray
记录:相当于结构体。
例子:
Declare
Type emp_record_type is record(
Name emp.ename%type,
Salary emp.sal%type,
Title emp.job%type);
Sp_record emp_record_type;
Begin
Select ename,sal,job in to sp_record
From emp where empno=7788;
Dbms_output.put_line('员工名:'||sp_record.name);
End;
/
测试:set serveroutput on;
表:相当于数组,但是他的下标可以为负数:
例子:
declare
type sp_table is table of emp.ename%type ---这个表记录雇员名字
index by binary_integer; ---按照整形排序
sp_t sp_table;
begin
select ename into sp_t(-2) from emp where emp no=7788; --把名字放到下标为-2的位置
dbms_output.put_line('雇员名字:'||sp_t(-2));--取这个位置的值
end;
嵌套表nested table和变长数组varray用的比较少。略。
参照变量:用于存放数值指针的变量,通过参照变量可以使得应用程序共享相同对象,从而降低占用的空间。
两种参照变量:
游标变量:refcursor:
定义游标时不需要指定相应的select 语句,但是当使用游标时(即打开游标时)需要指定select语句,使得他与一个select语句结合;
例子:
输入一个部门号,显示该部门的所有员工姓名和他的工资;
对象类型变量 ref obj_type:
declare
type sp_emp_cursor is ref cursor; //定义类型为游标类型
test_cursor sp_emp_cursor; //定义右边参照变量
v_name emp.ename%type;
v_sal emp.sal%type;
begin
open test_cursor for select ename ,sal from emp; //打开游标,与一个select语句相关联
loop //进行循环
fetch test_cursor into v_name,v_sal; //取出游标里的值到变量
exit when test_cursor% not found; 游标游到空时,退出
dbms_output.put_line('名字'||v_name||'工资'||v_sal); //
end loop; //关闭循环
close test_cursor; /关闭游标
end;