当前位置: 代码迷 >> SQL >> PL/SQL开发札记和小结(2)——PLSQL复合类型(转载)
  详细解决方案

PL/SQL开发札记和小结(2)——PLSQL复合类型(转载)

热度:61   发布时间:2016-05-05 12:13:32.0
PL/SQL开发笔记和小结(2)——PLSQL复合类型(转载)

*****************************************

PLSQL复合类型

*****************************************

记录类型record

record类型最常用,声明的时候可以加not null,但必须给初始值,如果record类型一致可以相互赋值,如果类型不同,里面的字段恰好相同,不能互相赋值。引用记录型变量的方法是记录变量名.基本类型变量名

? ―――――――――――――――――――――――――――――――――――――

? declare

?????? type t_first is record(

??????????? id number(3),

??????????? name varchar2(20)

?????? );

?????? v_first t_first;

? begin

???? v_first.id:=1;

???? v_first.name:='cheng';

???? dbms_output.put_line(v_first.id);

???? dbms_output.put_line(v_first.name);

? end;

? record类型变量间赋值

??declare

?????? type t_first is record(

??????????? id number(3),

??????????? name varchar2(20)

?????? );

?????? v_first t_first;

?????? v_second t_first;

? begin

?????? v_first.id:=1;

?????? v_first.name:='susu';

?

?????? v_second:=v_first;--相互赋值

?

?????? v_first.id:=2;

?????? v_first.name:='kettas';

?????? dbms_output.put_line(v_first.id);--2

?????? dbms_output.put_line(v_first.name);--kettas

?????? dbms_output.put_line(v_second.id);--1

?????? dbms_output.put_line(v_second.name);--susu

?? end;

?? ―――――――――――――――――――――――――――――――――――――

表类型变量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);

?? end;

?? ―――――――――――――――――――――――――――――――――――――

2. 定义多维表类型变量

该程序定义了名为tabletype1的多维表类型,相当于多维数组,table1是多维表类型变量,将数据表testtablerecordnumber60的记录提取出来存放在table1中并显示。

?? ―――――――――――――――――――――――――――――――――――――

?? declare

????? type tabletype1 is table of testtable%rowtype index by binary_integer;

????? table1 tabletype1;

?? begin

?????? select * into table1(60) from testtable where recordnumber=60;

?????? dbms_output.put_line(table1(60).recordnumber||'? '||table1(60).currentdate);

?? end;

?? 备注:在定义好的表类型变量里,可以使用countdeletefirstlastnextexistsprior等属性进行操作,使用方法为表变量名.属性,返回的是数字。

?? 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('总记录数:'||table1.count);--3

??????? dbms_output.put_line('第一条记录:'||table1.first);--1

??????? dbms_output.put_line('最后条记录:'||table1.last);--3

??????? dbms_output.put_line('第二条的前一条记录:'||table1.prior(2));--1

??????? dbms_output.put_line('第二条的后一条记录:'||table1.next(2));--3

??? 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 student.name%type;

??? begin

?????? select name into v_name from student where rownum=1;

?????? dbms_output.put_line(v_name);

??? 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(v_student.id||'? '||v_student.name||'? '||v_student.age);

??? end;

??? ―――――――――――――――――――――――――――――――――――――

??? 备注:insertupdatedeleteselect都可以,create tabledrop table不行。DPLDML,和流程控制语句可以在pl/sql里用,但DDL语句不行。

??? declare

?????? v_name student.name%type:='wang';

??? begin

?????? insert into student(id,name,age) values(2,v_name,26);

??? end;

?

??? declare

?????? v_name student.name%type:='hexian';

??? begin

?????? update student set name=v_name where id=1;

??? end;

*****************************************

???PLSQL变量的可见空间

*****************************************

变量的作用域和可见性,变量的作用域为变量声明开始到当前语句块结束。当外部过程和内嵌过程定义了相同名字的变量的时候,在内嵌过程中如果直接写这个变量名是没有办法访问外部过程的变量的,可以通过给外部过程定义一个名字<<outername>>,通过outername变量名来访问外部过程的变量(待测试..)。

??? ―――――――――――――――――――――――――――――――――――――

??? declare

??????????? v_i1 binary_integer:=1;

??? begin

???????? declare

??????????? v_i2 binary_integer:=2;

???????? begin

??????????? dbms_output.put_line(v_i1);

??????????? dbms_output.put_line(v_i2);

???????? end;

????? dbms_output.put_line(v_i1);

??? --dbms_output.put_line(v_i2);? 解开后执行Oracle会提示必须说明标识符 'V_I2'”

??? end;

?

转载自:http://www.blogjava.net/cheneyfree/archive/2008/07/19/216090.html?

  相关解决方案