/*
--建表
create table student(
recordid number(38),
sid int not null ,
sname varchar2(50),
sdate date,
sage number(3)
);
*/
--
删除表
--
drop table student;

--
插入数据
/*
set serveroutput on --允许服务器输出
declare
maxrecords constant int:=100;
i int:=1;
begin
for i in 1..maxrecords
loop
insert into student(sid,sdate)values(i,sysdate);
end loop
--dbms_output.put_line('成功录入数据!');
commit;
end;
*/
--
select * from student;
--
声明一个变量
/*
declare
pi constant number(9):=3.1415926;
begin
commit;
end;
*/
--
复合数据类型(常见的五种)
--
1 .使用 %type 定义变量
--
为了让PL/SQL中变量的类型和数据表中的字段的数据类型一致,Oracle 9i提供了%type定义方法。
--
这样当数据表的字段类型修改后,PL/SQL程序中相应变量的类型也自动修改.
/*
Declare
mydate student.sdate%type;
begin
commit;
end;
*/
--
2. 定义记录类型变量
--
将多个基本数据类型捆绑在一起的记录数据类型。
/*
set serveroutput on
declare
type myrecord is record(
sid int,
sdate date);
srecord myrecord; --声明一个自定义记录类型变量的实例
begin
select sid,sdate into srecord from student where sid=68;
dbms_output.put_line('ID: '|| srecord.sid ||'Date:'|| srecord.sdate); --'||': 它是字符串连接符.
end;
*/
--
3.使用 %rowtype 变量
--
使用%type可以使变量获得字段的数据类型,使用%rowtype可以使变量获得整个记录的数据类型。
--
比较两者定义的不同:变量名 数据表.列名%type,变量名 数据表%rowtype。
/*
set serveroutput on
Declare
mytableRow student%rowtype;
begin
select * into mytableRow
from student
where sid=88;
dbms_output.put_line(mytableRow.sid || mytableRow.sdate);
end;
*/
--
4.定义一维表类型变量
--
表类型变量和数据表是有区别的,定义表类型变量的语法如下:
--
―――――――――――――――――――――――――――――――――――――
--
type 表类型 is table of 类型 index by binary_integer;
--
表变量名 表类型;
--
―――――――――――――――――――――――――――――――――――――
--
类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
--
这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
/*
Declare
type tabletype1 is table of varchar2(4) index by binary_integer; --定义一个字符型的一维数组
type tabletype2 is table of student.sid%type index by binary_integer;--定义了一个整数数型的数组
table1 tabletype1; --实例声明
table2 tabletype2; --实例声明
begin
table1(1):='学生';
table1(2):='职员';
table2(1):=88;
table2(2):=89;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;
*/
--
5.定义多维类型表变量
--
相当于定义多维数组.
--
注意在运行下面的语句前要在数据库中插入数据.
Declare
type tabletype1
is
table
of
student
%
rowtype
index
by
binary_integer;
table1 tabletype1;
begin
select
*
into
table1(
60
)
from
student
where
sid
=
60
;
dbms_output.put_line(table1(
60
).sid
||
table1(
60
).sdate);
















































































































end;
原文:http://www.cnblogs.com/furenjun/archive/2006/07/04/plsql.html