表類型變量table
語法如下:
type 表類型 is table of 類型 index by binary_integer;
表變量名 表類型;
類型可以是前面的類型定義,index by binary_integer 子句代表以符號整數為索引,這樣訪問表
類型變量中的數據方法就是“表變量名(索引符號整數)”。table類型,相當於java中的Map容器,
就是一個可變長的數組,key(符號整數索引)必須是整數,可以是負數,value(類型)可以是
標量,也可以是record類型。可以不按順序賦值,但必須先賦值后使用。
1. 定義一維表類型變量
―――――――――――――――――――――――――――――――――――――
declare
type t_tb is table of varchar2(20) index by binary_integer;
v_tb t_tb;
begin
v_tb(100):='hello';
v_tb(98):='world';
dbms_output.put_line(v_tb(100));
dbms_output.put_line(v_tb(98));
end;
類型為record的表類型變量
declare
type t_rd is record(id number,name varchar2(20));
type t_tb is table of t_rd index by binary_integer;
v_tb2 t_tb;
begin
v_tb2(100).id:=1;
v_tb2(100).name:='hello';
--dbms_output.put_line(v_tb2(100).id);
--dbms_output.put_line(v_tb2(100).name);
dbms_output.put_line(v_tb2(100).id||' '||v_tb2(100).name);
end;
―――――――――――――――――――――――――――――――――――――
2. 定義多維表類型變量
該程序定義了名為tabletype1的多維表類型,相當於多維數組,table1是多維表類型變量,將數
據表tempuser.testtable中recordnumber為60的記錄提取出來
存放在table1中並顯示。
―――――――――――――――――――――――――――――――――――――
declare
type tabletype1 is table of testtable%rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1(60) from tempuser.testtable where recordnumber=60;
dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate);
end;
備注:在定義好的表類型變量里,可以使用count、delete、first、last、next、exists和prior
等屬性進行操作,使用方法為“表變量名.屬性”,返回的是數字。
set serveroutput on
declare
type tabletype1 is table of varchar2(9) index by binary_integer;
table1 tabletype1;
begin
table1(1):='成都市';
table1(2):='北京市';
table1(3):='青島市';
dbms_output.put_line('總記錄數:'||to_char(table1.count));
dbms_output.put_line('第一條記錄:'||table1.first);
dbms_output.put_line('最后條記錄:'||table1.last);
dbms_output.put_line('第二條的前一條記錄:'||table1.prior(2));
dbms_output.put_line('第二條的后一條記錄:'||table1.next(2));
end;
―――――――――――――――――――――――――――――――――――――
*****************************************
%type和%rowtype
*****************************************
使用%type定義變量,為了讓PL/SQL中變量的類型和數據表中的字段的數據類型一致,Oracle
9i提供了%type定義方法。這樣當數據表的字段類型修改后,PL/SQL程序中相應變量的類型也
自動修改。
―――――――――――――――――――――――――――――――――――――
create table student(
id number,
name varchar2(20),
age number(3,0)
);
insert into student(id,name,age) values(1,'susu',23);
--查找一個字段的變量
declare
v_name varchar2(20);
v_name2 student.name%type;
begin
select name into v_name2 from student where rownum=1;
dbms_output.put_line(v_name2);
end;
--查找多個字段的變量
declare
v_id student.id%type;
v_name student.name%type;
v_age student.age%type;
begin
select id,name,age into v_id,v_name,v_age from student where rownum=1;
dbms_output.put_line(v_id||' '||v_name||' '||v_age);
end;
--查找一個類型的變量,推薦用*
declare
v_student student%rowtype;
begin
select * into v_student from student where rownum=1;
dbms_output.put_line(v_student.id||' '||v_student.name||' '||v_student.age);
end;
--也可以按字段查找,但是字段順序必須一樣,不推薦這樣做
declare
v_student student%rowtype;
begin
select id,name,age into v_student from student where rownum=1;
dbms_output.put_line(v_student.id||' '||v_student.name||' '||v_student.age);
end;
declare
v_student student%rowtype;
begin
select id,name,age into v_student.id,v_student.name,v_student.age from student where
id=1;
--select * into v_student.id,v_student.name,v_student.age from student where id=1;
dbms_output.put_line();
end;
―――――――――――――――――――――――――――――――――――――
備注:insert,update,delete,select都可以,create table,drop table不行。DPL,DML,
和流程控制語句可以在pl/sql里用,但DDL語句不行。
declare
v_name student.name%type:='wang';
begin
insert into student(id,name,age) values(2,v_name,26);
end;
begin
insert into student(id,name,age) values(5,'hehe',25);
end;
declare
v_name student.name%type:='hexian';
begin
update student set name=v_name where id=1;
end;
begin
update student set name='qinaide' where id=2;
end;
―――――――――――――――――――――――――――――――――――――