您的位置:首页 > 数据库 > Oracle

Oracle学习笔记(二十七)——定义并使用变量

2016-04-19 13:55 344 查看
一、介绍
1、类型

标量类型(scalar)
复合类型(composite)
参照类型(reference)
lob(large object)

2、标量(scalar)
1)语法:

identifier:名称
constant:常量。需要制定初始值,而且不能够改变。
datatype:数据类型
not null
:=给变量或是常量指定初始值
default:指定初始值
expr:指定初始值的pl/sql表达式,可是文本值、其他变量、函数等

2)实例

定义一个变长字符串

v_ename varchar2(10);

定义一个小数,范围-9999.99~9999.99

v_sal number(6,2);

定义一个小树,并赋初始值为5.4

v_sal2 number(6,2):=5.4

定义一个日期类型的数据

v_hiredate date;

定义一个布尔变量,不能为空,初始值为false;

v_valid boolean not null default false;

3)使用

输入员工号,显示雇员姓名、工资、个人所得税(税率为0.03)

SQL> --输入员工号,显示雇员姓名、工资、个人所得税(税率为0.03)
SQL> declare
2  c_tax_rate number(3,2):=0.03;--税率
3  v_ename varchar2(5);--用户名
4  v_sal number(7,2);
5  v_tax_sal number(7,2);
6  begin
7    --执行部分
8    select ename,sal into v_ename,v_sal from emp where empno=&no;
9    --计算所得税
10    v_tax_sal:=v_sal*c_tax_rate;
11    --输出
12    dbms_output.put_line('姓名:'||v_ename||',工资:'||v_sal||',个人所得税为:'||v_tax_sal);
13  end;
14  /
姓名:SCOTT,工资:1500,个人所得税为:45
PL/SQL procedure successfully completed


4)使用%type类型

对于上面的pl/sql块:如果员工的姓名超过了5字符的话,就会有错误。为了降低pl/sql程序的维护工作量,可以使用%type属性定义变量。这样,可以按照数据库列来确定定义的变量的类型和长度。

ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小

用法:

标识符名 表名.列名%type;
v_ename emp.ename%type;
--用户名

3、复合变量(composite)
1)用于存放多个值的变量:

pl/sql记录
pl/sql表
嵌套表
varray(动态表)

2)pl/sql记录

类似高级语言中的结构体、类。
注意:当引用pl/sql记录成员时,必须要加记录变量作为前缀(记录变量.记录成员)。
SQL> --pl/sql记录实例
SQL> declare
2  --定义一个pl/sql记录类型emp_record_type,类型包含三个数据name,salary,title
3  type emp_record_type is record(
4  name emp.ename%type,
5  salary emp.sal%type,
6  title emp.job%type);
7  --定义了一个变量sp_record,类型是emp_record_type
8  sp_record emp_record_type;
9  begin
10    select ename,sal,job into sp_record from emp where empno=7788;
11    dbms_output.put_line('员工名:'||sp_record.name||',工资是:'||sp_record.salary||',工作是:'||sp_record.title);
12  end;
13  /
员工名:SCOTT,工资是:1500,工作是:CLERK
PL/SQL procedure successfully completed


3)pl/sql表

相当于高级语言中的数组。
注意:在高级语言中数组的下标不能为负数,而pl/sql的下标是可以为负数的,并且表元素的下标没有限制。
SQL> --pl/sql表实例
SQL> declare
2  --定义了一个pl/sql表类型sp_table_type,该类型是用于存放emp.ename%type
3  --index by binary_integer表示下标为整数
4  type sp_table_type is table of emp.ename%type index by binary_integer;
5  --定义了变量sp_table,类型是sp_table_type
6  sp_table sp_table_type;
7  begin
8    select ename into sp_table(0) from emp where empno=7788;
9    dbms_output.put_line('员工名'||sp_table(0));
10  end;
11  /
员工名SCOTT
PL/SQL procedure successfully completed


说明:

sp_table_type是pl/sql表类型
emp.ename%type制定了表的元素的类型和长度
sp_table为pl/sql表变量
sp_table(0)表示下表为0的元素

如果去掉where条件:

SQL> --pl/sql表实例
SQL> declare
2  --定义了一个pl/sql表类型sp_table_type,该类型是用于存放emp.ename%type
3  --index by binary_integer表示下标为整数
4  type sp_table_type is table of emp.ename%type index by binary_integer;
5  --定义了变量sp_table,类型是sp_table_type
6  sp_table sp_table_type;
7  begin
8    select ename into sp_table(0) from emp;
9    dbms_output.put_line('员工名'||sp_table(0));
10  end;
11  /
ORA-01422: 实际返回的行数超出请求的行数
ORA-06512: 在 line 8


4)嵌套表(nested table)
5)边长数组(varray)

4、参照变量
1)参照变量

用于存放数值指针的变量。
是应用程序共享相同对象,从而降低占用的空间。
分为:游标变量(ref cursor)和对象类型变量(ref obj_type)。

2)游标变量(ref cursor)

定义游标时,不需要指定相应的select语句。
使用游标(open)时,需要制定select语句。
实例:

使用pl/sql编写一个块,可以输入部门号,并显示该部门所有员工姓名和工资。
SQL> --使用pl/sql编写一个块,可以输入部门号,并显示该部门所有员工姓名和工资。
SQL> declare
2  --定义游标类型
3  type sp_emp_cursor is ref cursor;
4  --定义一个游标变量
5  test_cursor sp_emp_cursor;
6  --定义变量
7  v_ename emp.ename%type;
8  v_sal emp.sal%type;
9  begin
10    --执行
11    --把test_cursor和一个select结合
12    open test_cursor for select ename,sal from emp where deptno=&no;
13    --循环取出
14    loop
15      fetch test_cursor into v_ename,v_sal;
16      --判断是否test_cursor为空
17      exit when test_cursor%notfound;
18      dbms_output.put_line('姓名:'||v_ename||',薪水:'||v_sal);
19    end loop;
20  end;
21  /

姓名:小红,薪水:78.9
姓名:小红2,薪水:78.9
姓名:CLARK,薪水:2450
姓名:KING,薪水:5000
姓名:MILLER,薪水:1300
姓名:ok,薪水:34.34
姓名:test用户,薪水:23
PL/SQL procedure successfully completed

在上面的基础上,如果某个员工的工资低于200元,则增加100元。
SQL> --使用pl/sql编写一个块,可以输入部门号,并显示该部门所有员工姓名和工资。
SQL> declare
2  --定义游标类型
3  type sp_emp_cursor is ref cursor;
4  --定义一个游标变量
5  test_cursor sp_emp_cursor;
6  --定义变量
7  v_ename emp.ename%type;
8  v_sal emp.sal%type;
9  begin
10    --执行
11    --把test_cursor和一个select结合
12    open test_cursor for select ename,sal from emp where deptno=&no;
13    --循环取出
14    loop
15      fetch test_cursor into v_ename,v_sal;
16      --判断工资高低,决定是否更新
17      if v_sal<200 then
18        update emp set sal=sal+100 where ename=v_ename;
19      end if;
20      fetch test_cursor into v_ename,v_sal;
21      --判断是否test_cursor为空
22      exit when test_cursor%notfound;
23      dbms_output.put_line('姓名:'||v_ename||',薪水:'||v_sal);
24    end loop;
25  end;
26  /
姓名:小红,薪水:178.9
姓名:小红2,薪水:178.9
姓名:KING,薪水:5000
姓名:ok,薪水:134.34
姓名:test用户,薪水:123
PL/SQL procedure successfully completed
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: