oracle14 复杂数据类型
复合类型-pl/sql表类型 相当于高级语言中的数组,但是需要注意的是在高级语言中数组的下标不能为负数,而pl/sql是可以为负数的,并且表元素的下标没有限制。实例如下: Sql代码 1.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 5. index by binary_integer; 6.--定义了一个sp_table变量,这个变量的类型是sp_table_type 7. sp_table sp_table_type; 8.begin 9. select ename into sp_table(-1) from emp where empno = 7788; 10. dbms_output.put_line('员工名:' || sp_table(-1)); 11.end;
说明: sp_table_type 是pl/sql表类型 emp.ename%type 指定了表的元素的类型和长度 sp_table 为pl/sql表变量 sp_table(0) 则表示下标为0的元素 注意:如果把select ename into sp_table(-1) from emp where empno = 7788;变成select ename into sp_table(-1) from emp;则运行时会出现错误,错误如下: ORA-01422:实际返回的行数超出请求的行数 解决方法是:使用参照变量(这里不讲)
复合变量——嵌套表(nested table)
复合变量——变长数组(varray)
参照变量 参照变量是指用于存放数值指针的变量。通过使用参照变量,可以使得应用程序共享相同对象,从而降低占用的空间。在编写pl/sql程序时,可以使用游标变量(ref cursor,用的最多)和对象类型变量(ref obj_type)两种参照变量类型。 参照变量——ref cursor游标变量 使用游标时,当定义游标时不需要指定相应的select语句,但是当使用游标时(open时)需要指定select语句,这样一个游标就与一个select语句结合了。实例如下: 1.请使用pl/sql编写一个块,可以输入部门号,并显示该部门所有员工姓名和他的工资。 2.在1的基础上,如果某个员工的工资低于200元,就添加100元。 Sql代码 1.declare 2.--定义游标sp_emp_cursor类型 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; /
pl/sql的进阶--控制结构(分支,循环,控制) pl/sql的进阶--控制结构 在任何计算机语言(c,java,pascal)都有各种控制语句(条件语句,循环结构,顺序控制结构...)在pl/sql中也存在这样的控制结构。 在本部分学习完成后,希望大家达到: 1.使用各种if语句 2.使用循环语句 3.使用控制语句——goto和null; 条件分支语句 pl/sql中提供了三种条件分支语句if—then,if – then – else,if – then – elsif – then 这里我们可以和java语句进行一个比较 简单的条件判断 if – then 问题:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。 Sql代码 1.create or replace procedure sp_pro6(spName varchar2) is 2.--定义 3.v_sal emp.sal%type; 4.begin 5. --执行 6. select sal into v_sal from emp where ename=spName; 7. --判断 8. if v_sal<2000 then 9. update emp set sal=sal+sal*10% where ename=spName; 10. end if; 11.end; 12./ 二重条件分支 if – then – else 问题:编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0就在原来的基础上增加100;如果补助为0就把补助设为200; Sql代码 1.create or replace procedure sp_pro6(spName varchar2) is 2.--定义 3.v_comm emp.comm%type; 4.begin 5. --执行 6. select comm into v_comm from emp where ename=spName; 7. --判断 8. if v_comm<>0 then 9. update emp set comm=comm+100 where ename=spName; 10. else 11. update emp set comm=comm+200 where ename=spName; 12. end if; 13.end; 14./
多重条件分支 if – then – elsif – then 问题:编写一个过程,可以输入一个雇员编号,如果该雇员的职位是PRESIDENT就给他的工资增加1000,如果该雇员的职位是MANAGER就给他的工资增加500,其它职位的雇员工资增加200。 Sql代码 1.create or replace procedure sp_pro6(spNo number) is 2. --定义 3. v_job emp.job%type; 4.begin 5. --执行 6. select job into v_job from emp where empno=spNo; 7. if v_job='PRESIDENT' then 8. update emp set sal=sal+1000 where empno=spNo; 9. elsif v_job='MANAGER' then 10. update emp set sal=sal+500 where empno=spNo; 11. else 12. update emp set sal=sal+200 where empno=spNo; 13. end if; 14.end; 15./
循环语句 –loop 是pl/sql中最简单的循环语句,这种循环语句以loop开头,以end loop结尾,这种循环至少会被执行一次。 案例:现有一张表users,表结构如下: 用户id | 用户名 | 请编写一个过程,可以输入用户名,并循环添加10个用户到users表中,用户编号从1开始增加。 Sql代码 1.create or replace procedure sp_pro6(spName varchar2) is 2.--定义 :=表示赋值 3. v_num number:=1; 4.begin 5. loop 6. insert into users values(v_num,spName); 7. --判断是否要退出循环 8. exit when v_num=10; 9. --自增 10. v_num:=v_num+1; 11. end loop; 12.end; /
循环语句 –while循环 基本循环至少要执行循环体一次,而对于while循环来说,只有条件为true时,才会执行循环体语句,while循环以while...loop开始,以end loop结束。 案例:现有一张表users,表结构如下: 用户id 用户名 问题:请编写一个过程,可以输入用户名,并循环添加10个用户到users表中,用户编号从11开始增加。 Sql代码 1.create or replace procedure sp_pro6(spName varchar2) is 2.--定义 :=表示赋值 3. v_num number:=11; 4.begin 5. while v_num<=20 loop 6. --执行 7. insert into users values(v_num,spName); 8. v_num:=v_num+1; 9. end loop; 10.end; 11./
循环语句 –for循环 基本for循环的基本结构如下 Sql代码 1.begin 2. for i in reverse 1..10 loop 3. insert into users values (i, 'shunping'); 4. end loop; 5.end; 我们可以看到控制变量i,在隐含中就在不停地增加。
顺序控制语句 –goto,null 1.goto语句 goto语句用于跳转到特定符号去执行语句。注意由于使用goto语句会增加程序的复杂性,并使得应用程序可读性变差,所以在做一般应用开发时,建议大家不要使用goto语句。 基本语法如下 goto lable,其中lable是已经定义好的标号名, Sql代码 1.declare 2. i int := 1; 3.begin 4. loop 5. dbms_output.put_line('输出i=' || i); 6. if i = 1{} then 7. goto end_loop; 8. end if; 9. i := i + 1; 10. end loop; 11. <<end_loop>> 12. dbms_output.put_line('循环结束'); 13.end; 2.null null语句不会执行任何操作,并且会直接将控制传递到下一条语句。使用null语句的主要好处是可以提高pl/sql的可读性。 Sql代码 1.declare 2. v_sal emp.sal%type; 3. v_ename emp.ename%type; 4.begin 5. select ename, sal into v_ename, v_sal from emp where empno = &no; 6. if v_sal < 3000 then 7. update emp set comm = sal * 0.1 where ename = v_ename; 8. else 9. null; 10. end if; end;