博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Oracle】Oracle中复合数据类型
阅读量:4696 次
发布时间:2019-06-09

本文共 5127 字,大约阅读时间需要 17 分钟。

1,常见的操作数据库的技术有那些

   jdbc     使用java 访问数据库的技术

   PLSQL  (procedure  过程化sql) 在数据库内部操作数据的技术
   proc/c++    c 和 c++ 访问数据库的技术
   ODBC      微软提供访问数据库的技术
   OCI          oracle  底层的连接接口 

2,Oracle中的复合数据类型

Oracle中的复合数据类型record,table,cursor

2.1 record类型

2.1.1 语法

/*定义一个record类型*/           type record类型名称 is record(                属性  数据类型,                属性  数据类型                ...           );           /*使用record类型名称定义一个record变量*/           record变量名   record类型名称

也可以使用表的字段来定义,

/*使用表的字段类型定义一个record类型*/           type record类型名称 is record(                属性  表名.属性名%type,                属性  表名.属性名%type                ...           );           /*使用record类型名称定义一个record变量*/           record变量名   record类型名称

2.1.2 示例

使用record类型存储来存储不同类型的数据:

set serveroutput on;declare           type  emptype  is  record(                  id     number,                  name varchar2(5)            );           var_emp   emptype;begin           var_emp.id:=1;           var_emp.name:='jame';           dbms_output.put_line(var_emp.id);end;/

2.2 table类型

2.2.1 语法

/*定义一个table类型*/type    table类型名  is  table  of  元素类型名    index  by  binary_integer;/*定义一个table变量*/变量名      table类型名;

访问数据的方式,

table变量(下标):=值;

2.2.2 示例

使用表格存储同一种类型的数据:

declare              type  numstype  is table of  number   index by binary_integer;              var_nums   numstype;                     begin              var_nums(0):=9;              var_nums(1):=5;              var_nums(2):=2;              var_nums(3):=7;              var_nums(4):=0;              dbms_output.put_line(var_nums(3));/*打印下标为3的元素*/      end;

table中的数据也可以使用迭代的思想来操作

     下标不连续时 遍历table类型的变量      

      迭代思想:
      变量名.first()   获取第一个元素对应的下标 (元素对应的最小下标)
      变量名.next(n)  根据一个元素的下标n  得到下一个元素对应的下标
      变量名.last()     获取最后一个元素对应的下标  

例如,使用迭代遍历表中的所有数据:

set serveroutput on;       declare              type  numstype  is table of  number   index by binary_integer;              var_nums   numstype;               var_index    binary_integer:=0;                    begin              var_nums(0):=9;              var_nums(1):=5;              var_nums(-12):=2;              var_nums(3):=7;              var_nums(4):=0;              -- var_nums.count();               var_index :=  var_nums.first();              loop                      dbms_output.put_line(var_nums(var_index));                      var_index:=var_nums.next(var_index);                      if var_index =  var_nums.last()  then                             dbms_output.put_line(var_nums(var_index));                             exit;                       end if;                end loop;       end;     /

 

2.3 cursor类型

2.3.1 语法

/*声明游标*/ cursor   游标名   is  select语句;/*打开游标*/open   游标名;/*提取数据,将结果存储到变量中,一般都是record类型*/fetch   游标名   into  变量;/*关闭游标*/close 游标名

2.3.2 游标的属性

% ISOPEN 判断游标是否打开,若打开就返回true,否则就返回false.

% ROWCOUNT 当前游标指针的偏移量.

%FOUND 如果游标提取到新数据,就返回true,否则就返回false.

%NOTFOUND 如果游标没有提取到新数据,就返回true,否则就返回false.

2.3.3 示例

遍历一个游标中的所有数据行:

declare            /*使用s_emp创建一个游标类型*/          cursor   empcursor   is  select  id,first_name,salary from   s_emp;          /*使用游标创建一个record变量*/          var_emp   empcursor % rowtype;   begin          open    empcursor;          loop                    fetch    empcursor  into     var_emp;                   /* 如果没有提取到 新数据  则结束循环 */                   exit     when     empcursor%notfound;                   dbms_output.put_line(var_emp.id||':'||var_emp.first_name||':'||var_emp.salary);          end  loop;          close    empcursor;   end;   //*   --也可以使用如下的的for循环快速的遍历游标中的数据   --使用for循环可以自动声明游标变量,可以自动打开游标,可以自动提取数据,可以自动关闭游标   --在调用游标类型的时候可以传递参数,上面的例子可以在打开游标的时候传入参数"open    empcursor(20);"   declare            cursor   empcursor(var_id   number)   is  select  id,first_name,salary from   s_emp where  id> var_id;   begin         FOR   VAR_EMP IN  empcursor(20) LOOP                    dbms_output.put_line(var_emp.id||':'||var_emp.first_name||':'||var_emp.salary);          END  LOOP;   end;   /*/

2.3.4 参考游标 refer cursor

使用参考游标, 游标对应的sql语句,可以在程序执行的过程中发生改变 ,直到打开游标时确定对应的sql语句。

--定义一个参考游标类型  type    参考游标类型名   is   ref  cursor;--使用这个类型  定义一个游标变量 游标变量    参考游标类型名;--打开游标时 关联一个动态拼接好的字符串 open  游标变量  for   SQL的字符串;

例如:

根据条件动态指定查询的数据,并且遍历所有得到的数据行:

declare           /*创建一个参考游标类型*/           type    myrefcursor  is   ref  cursor;           /*创建一个参考游标变量*/           refempcursor    myrefcursor;           /* 为这个参考游标  定义一个动态字符串 */           sqlstr    varchar2(200);           var_id    number:=10;           type     emptype   is  record(                   id     s_emp.id%type,                   salary  s_emp.salary % type,                   dept_id  s_emp.dept_id%type           );           var_emp    emptype;    begin           sqlstr := 'select  id,salary,dept_id  from  s_emp';           if  var_id  !=  0  then                     sqlstr := sqlstr ||' where id > ' || var_id;           end  if;               open    refempcursor  for  sqlstr;           loop                  fetch   refempcursor  into  var_emp;                  exit  when  refempcursor%notfound;                  dbms_output.put_line(var_emp.id||':'||var_emp.salary||':'||var_emp.dept_id);           end  loop;           close   refempcursor;    end;    /

 

转载于:https://www.cnblogs.com/HDK2016/p/6901709.html

你可能感兴趣的文章
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>
RAMPS1.4 3d打印控制板接线与测试1
查看>>
python with语句中的变量有作用域吗?
查看>>
24@Servlet_day03
查看>>
初级ant的学习
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>