当前位置:文章写作网 >日记 >日记 >oracle学习笔记-plsql变量介绍

oracle学习笔记-plsql变量介绍

2008-11-27 13:49 作者:tshfang 阅读量:10452 推荐14次 | 我要投稿

1、标量变量

存放当个数值的变量。定义标量变量,使用标量变量,在此不作过多介绍。

2、复合变量

(1)记录:和c语言中的结构类似。

SET serveroutput on

declare

type dept_record_type is record

(

deptno SCOTT.DEPT.DEPTNO%type,

deptname SCOTT.DEPT.DNAME%type,

deptloc SCOTT.DEPT.LOC%type

);/*定义记录类型*/

dept dept_record_type;/*定义记录变量*/

begin

select deptno,dname,loc into dept

from SCOTT.DEPT

where deptno='10';

DBMS_OUTPUT.put_line ('部门名称是:'||dept.deptname);

end;/*使用记录变量*/

/

运行结果:

部门名称是:ACCOUNTING

PL/SQL 过程已成功完成。

SQL>

(2)表:类似c语言的数据。但是pl/sql语言的数组下表可以为负数。

SET serveroutput on

declare

type dept_table_type is table of SCOTT.DEPT.DNAME%type

index by binary_integer;

/*定义数组类型*/

dept dept_table_type;/*定义数组变量*/

begin

select dname into dept(-2)

from SCOTT.DEPT

where deptno='20';

DBMS_OUTPUT.put_line ('部门名称是:'||dept(-2));

end;/*使用数组变量*/

/

结果如下:

部门名称是:RESEARCH

PL/SQL 过程已成功完成。

SQL>

(3)嵌套表:类似c中的数组,和表不一样嵌套表下标不能为负值。嵌套表的元素个数没有限制。嵌套表能够作为数据类型。

例如:

create or replace type dept_type as object

(

no varchar(20),name varchar(50),loc,varchar(20)

);

/*定义嵌套表数据类型*/

create or replace type dept is table of dept_type;

/*定义基于dept_type嵌套表的类型*/

(4)varray:类似嵌套表。varray元素的个数有限。

create or replace type dept_type as object

(

no varchar(20),name varchar(50),loc,varchar(20)

);

/*定义varray数据类型*/

create or replace type dept is varray(20) of dept_type;

/*定义基于dept_type varry的类型*/

3、参照变量

参照变量是用于存放数值指针的变量。使用参照标量可以共享相同对象,降低占用空间。

游标变量(ref cursor):在定义游标变量的时候不需要指定select语句,而是在打开游标时候指定select语句。

例如:

SET serveroutput on

declare

type cur1 is ref cursor;

dept_cursor cur1;

name SCOTT.DEPT.DNAME%type;

loc SCOTT.DEPT.LOC%type;

begin

open dept_cursor for

select T.DNAME,T.LOC

from scott.dept T;

loop

fetch dept_cursor into name,loc;

exit when dept_cursor%notfound;

dbms_output.put_line(name||' '||loc);

end loop;

close dept_cursor;

end;

/

ACCOUNTING NEW YORK

RESEARCH DALLAS

SALE CHICAGO

OPERATIONS BOSTON

PL/SQL 过程已成功完成。

SQL>

4、lob变量

lob用于存储大批量数据的变量。

(1)内部lob变量。clob,blob,nclob三种,三种类型的数据被存储在数据库中,并且支持事务操作(提交,回退,保存点)。

(2)外部lob变量。只有一种bfile类型,存储在os文件中。不支持事务操作。

其他人在看啥

    《oracle学习笔记-plsql变量介绍》的评论 (共 0 条)

    • Guest::piaoliang
    • Guest::cool