标签:存储 binary integer to_char end span 索引 tput class
1.案例: 通过record类型处理一行多列的数据
复合变量--record 记录类型
record:在一个变量中可以存储多个区域,每个区域可以是一个标量、记录或table (用于处理一行多列的信息)
SQL> declare
2 type emp_record is record //定义变量类型为record
3 ( v_ename emp.ename%type,
4 v_sal emp.sal%type,
5 v_deptno emp.deptno%type
6 );
7
8 emp_rcd emp_record ;
9
10 begin
11
12 select ename,sal,deptno into emp_rcd from emp where empno=#
13
14 dbms_output.put_line (‘Employees name is: ‘||emp_rcd.v_ename);
15 dbms_output.put_line (‘Employees salary is: ‘||emp_rcd.v_sal);
16 dbms_output.put_line (‘Employees deparment number is: ‘||emp_rcd.v_deptno);
17 end;
2.通过%rowtype处理一行数据
SQL> declare
2 emp_rcd emp%rowtype ;
3
4 begin
5
6 select * into emp_rcd from emp where empno=#
7
8 dbms_output.put_line (‘Employees name is: ‘||emp_rcd.ename);
9 dbms_output.put_line (‘Employees salary is: ‘||emp_rcd.sal);
10 dbms_output.put_line (‘Employees hiredate is: ‘||to_char(emp_rcd.hiredate,‘yyyy-mm-dd‘));
11 dbms_output.put_line (‘Employees deparment number is: ‘||emp_rcd.deptno);
12 end;
3.案例:通过table类型变量处理一列多行的数据
table 类型(处理单列多行数据)
table: 相当于高级语言中的数组,但是需要注意的是在高级语言中数组的下标不能为负数,而pl/sql是可以为负数的,并且表元素的下标没有限制
(处理单列多行数据)
SQL> declare
2 type emp_tab is table of emp.ename%type //定义变量类型为table类型
3 index by binary_integer; //建立索引
4
5 emp_table emp_tab;
6
7 begin
8
9 select ename into emp_table(1) from emp where empno=7788;
10 select ename into emp_table(2) from emp where empno=7369;
11 select ename into emp_table(3) from emp where empno=7499;
12
13 dbms_output.put_line(‘Employees 7788 name is: ‘||emp_table(1));
14 dbms_output.put_line(‘Employees 7369 name is: ‘||emp_table(2));
15 dbms_output.put_line(‘Employees 7499 name is: ‘||emp_table(3));
16 end;
SQL> declare
2 type emp_tab is table of emp.ename%type
3 index by binary_integer;
4
5 type emp_t is table of emp.sal%type
6 index by binary_integer;
7
8 emp_table emp_tab;
9 emp_t_sal emp_t;
10
11 begin
12
13 select ename into emp_table(1) from emp where empno=7788;
14 select ename into emp_table(2) from emp where empno=7369;
15 select ename into emp_table(3) from emp where empno=7499;
16
17 select sal into emp_t_sal(1) from emp where empno=7788;
18 select sal into emp_t_sal(2) from emp where empno=7369;
19 select sal into emp_t_sal(3) from emp where empno=7499;
20
21 dbms_output.put_line(‘Employees 7788 name is: ‘||emp_table(1));
22 dbms_output.put_line(‘Employees 7369 name is: ‘||emp_table(2));
23 dbms_output.put_line(‘Employees 7499 name is: ‘||emp_table(3));
24
25 dbms_output.put_line(‘Employees 7788 salary is: ‘||emp_t_sal(1));
26 dbms_output.put_line(‘Employees 7369 salary is: ‘||emp_t_sal(2));
27 dbms_output.put_line(‘Employees 7499 salary is: ‘||emp_t_sal(3));
28 end;
4.在table类型里嵌套record类型 ,可以处理多行多列的数据
SQL> declare
2 type emp_table_type is table of emp%rowtype
3 index by binary_integer;
4 emp_tab emp_table_type;
5
6 begin
7 select * into emp_tab(1) from emp where empno=7788;
8 select * into emp_tab(2) from emp where empno=7369;
9 dbms_output.put_line(‘7788 Ename is: ‘||emp_tab(1).ename );
10 dbms_output.put_line(‘7788 Salary is: ‘|| emp_tab(1).sal);
11 dbms_output.put_line(‘7788 Deparment ID is: ‘||emp_tab(1).deptno);
12 dbms_output.put_line(‘7369 Ename is: ‘||emp_tab(2).ename);
13 dbms_output.put_line(‘7369 Salary is: ‘|| emp_tab(2).sal);
14 dbms_output.put_line(‘7369 Deparment ID is: ‘||emp_tab(2).deptno);
15 end;
【PL/SQL练习】复合变量: 可以一次传递多个值到变量中。
标签:存储 binary integer to_char end span 索引 tput class
原文地址:http://www.cnblogs.com/tomatoes-/p/6104233.html