`

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 

分享到:
评论

相关推荐

    pl/sql developer11.0

    pl/sql developer11.0下载 pl/sql developer11.0下载 pl/sql developer11.0下载

    PL/SQL developer 12.07 注册码 可以使用

    PL/SQL developer 12.07 注册码 可以使用,不错,自己用过了,分享给大家

    PL/SQL Developer9.06

    如今,有越来越多的商业逻辑和应用逻辑转向了Oracle Server,因此,PL/SQL编程也成了整个开发过程的一个重要组成部分。PL/SQL Developer侧重于易用性、代码品质和生产力,充分发挥Oracle应用程序开发过程中的主要...

    oracle PL/SQL测试题目和详细答案

    pl/sql存储过程,函数,游标,以及存储过程中的基础知识,绝对值得你收藏的经典题目,让你的pl/sql得到最大的锻炼。让你的数据库逻辑更加灵活。

    pl/sql快捷插件

    pl/sql插件,下载,解压,将一个文件夹和一个dll文件直接放在pl/sql安装目录的 plugin目录下,打开pl/sql便可在工具栏看到plugin,然后可以按照自己的需求设置

    DBAtools for PL/SQL表空间管理器

    PL/SQL Developer是Oracle数据库当前最流行的开发工具之一,它在ORACLE数据库开发设计方面功能强大,使用方便,但是数据库管理方面一直比较欠缺。 DBATools For PL/SQL Developer 是一款PL/SQL Developer的辅助插件...

    精通Oracle 10g SQL和PL/SQL

    本书是专门为oracle开发人员而提供的编程指南 通过学习本书 读者不仅可以掌握编写sql语句和pl/sql块的基础知识 而且还可以掌握sql高级特征 正则表达式 flashback查询 merge语句 sql:1999连接 和pl/sql高级特征 ...

    PL/SQL Developer v8.0.3 1510

    PL/SQL Developer 8.0.3 1510 含注册机 PL/SQL Developer is an Integrated Development Environment that is specifically targeted at the development of stored program units for Oracle Databases. Over ...

    Oracle PL SQL程序设计 上 第五版(代码示例)

    《oracle pl/sql程序设计(第5版)》基于oracle数据库11g,从pl/sql编程、pl/sql程序结构、pl/sql程序数据、pl/sql中的sql、pl/sql应用构建、高级pl/sql主题6个方面详细系统地讨论了pl/sql以及如何有效地使用它。...

    PLSQL安装包

    PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用。PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)。PL/SQL是Oracle数据库对SQL语句的扩展。在普通SQL语句的使用上增加...

    PL/SQL美化器规则文件,用于格式美化sql语句

    PL/SQL使用方法: 1、工具--首选项--用户界面--PL/SQL美化器--规则文件,选择.br的规则文件,点确定按钮。 2、点击工具栏的“PL/SQL美化器”,会自动把一段SQL语句重新排版美化。

    PL/SQL编程基础知识

    PL/SQL 编程基础知识详解,PL/SQL 包含过程化语句和SQL语句数据操作和查询语句被包含在PL/SQL代码的程序单元中(PL/SQL块),经过逻辑判断、循环等操作完成复杂的功能或者计算.。

    pl/sql64位

    很多时候你是不是为了32为的plsql的各种复杂配置烦恼,不要紧,现在下载64位的pl/sql,不需要繁琐的配置,让你更轻松

    PL/SQL 超级好用插件 CnPlugin

    PL/SQL 插件。...2.安装到pl/sql 安装目录下的PlugIns目录下; 比如我的: D:\Program Files (x86)\PLSQL Developer\PlugIns 安装到此目录下; 3.重启pl/sql; 4.在页面中的plugins--cnplugin进行设置PL/SQL;

    PL/SQL Developer 客户端

    PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用。PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)。PL/SQL是Oracle数据库对SQL语句的扩展。在普通SQL语句的使用上增加...

    PL/SQL Developer 12 舒爽版

    PL/SQL Developer 12 舒爽版,用了都说好。...2、根据本文链接下载 PL/SQL Developer 12 舒爽版 3、安装汉化文件 chinsese.exe 4、将授权文件放到 C:\Users\用户名\AppData\Roaming\PLSQL Developer 12 下。

    pl/sql开发手册

    pl/sql开发手册,适用于ORACLE初学者

    Oracle_PLSQL语言基础

    PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL基础语法,结构和组件、以及如何设计并执行一个PL...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二)

     除了为读者提供编写sql语句和开发pl/sql块的方法外,本书还为应用开发人员提供了一些常用的pl/sql系统包。通过使用这些pl/sql系统包,应用开发人员可以开发出功能更强大的数据库应用程序。本书不仅适合sql和pl/sql...

    oracle-plsql

    2、PL/SQL语言集成了面向过程语言的过程结构和强大的数据库操作,为设计复杂的数据库应用提供了功能强大、健壮可靠的程序设计语言 3、PL/SQL语言专门用于在各种环境下对Oracle数据库进行访问。由于PL/SQL语言集成于...

Global site tag (gtag.js) - Google Analytics