Oracle笔记:变量

1.标量类型(scalar)
例:
--输入员工编号,显示雇员姓名、工资、个人所得税(税率为0.03)
declare
v_name emp.ename%type;
v_sal emp.sal%type;
v_tax number(7,2);
c_taxrate number(3,2):=0.03;
begin
select ename,sal into v_name,v_sal from emp where empno=&no;
v_tax:=v_sal*c_taxrate;
dbms_output.put_line('员工姓名:'||v_name||',工资:'||v_sal||',个人所行税:'||v_tax);
end;

2.复合类型(composite):用于存放多个值的变量,主要包括以下几种:1)pl/sql记录 2)pl/sql表 3)嵌套表 4)varray。
1)pl/sql记录:类似于高级语言的结构体。注意:引用pl/sql记录成员时,必须要加记录变量作为前缀(记录变量.记录成员)。例:
declare
--定义一个pl/sql记录类型emp_record_type,包含三个成员。
type emp_record_type is record(
name emp.ename%type,
salary  emp.sal%type,
title emp.job%type);
--定义变量sp_record。
test_record emp_record_type;
begin
select ename,sal,job into test_record from emp where empno=&no;
dbms_output.put_line('员工姓名:'||test_record.name||' 工作岗位:'||test_record.title);
end;

2)pl/sql表:类似高级语言中的数组。注意:pl/sql表元素的下标可以为负数,且下标没有限制。例:
declare
--定义一个pl/sql表类型emp_table_type,该类型用于存放emp.ename%type。
--表的表元素下标是整数。
type emp_table_type is table of emp.ename%type index by binary_integer;
--定义变量sp_record。
test_table emp_table_type;
begin
select ename into test_table(0) from emp where empno=&no;
dbms_output.put_line('员工姓名:'||test_table(0));
end;

3.参照类型(reference):用于存放数值指针的变量,通过使用参照变量,可以使应用程序共享相同对象,从而降低占用的空间,在编写pl/sql程序时,可以使用游标变量(ref cursor)和对象类型变量(ref obj_type)两种参照变量类型。
1)游标变量(ref cursor):使用游标时,当定义游标时不需要指定相应的select语句,但是当使用游标(open)时需要指定select语句。例:
--编写一个块,输入部门号,并显示该部门所有员工姓名和工资
declare
--定义游标类型
type test_emp_cursor is ref cursor;
--定义一个游标变量
test_cursor test_emp_cursor;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
 --把游标变量指向select结果集
open test_cursor for select ename,sal from emp where deptno=&no;
--循环取出
loop
   fetch test_cursor into v_ename,v_sal;
   --判断test_cursor是否为空
   exit when test_cursor%notfound;
   dbms_output.put_line('姓名:'||v_ename||' 工资:'||v_sal); 
end loop;
end;

4.lob(large object)

原文地址:https://www.cnblogs.com/testing/p/3001888.html