1. 程式人生 > >【PLSQL】PLSQL中復合數據類型

【PLSQL】PLSQL中復合數據類型

next 存儲 int 微軟 sel acl 遊標 復合 lec

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 示例

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 示例

         declare 
                /*使用s_emp創建一個遊標變量*/
                cursor  empcursor  is  select  id,first_name name,salary from s_emp;
                /*創建一個record類型*/
                type   emptype is  record(
                       id    s_emp.id%type,
                       name s_emp.first_name%type,
                       salary  s_emp.salary%type
                );
                /*創建一個record變量*/
                var_emp   emptype;
         begin
                /*打開遊標*/
                open   empcursor;

                /*將遊標的第一條值存儲到var_emp中*/
                fetch   empcursor  into  var_emp;
                dbms_output.put_line(var_emp.id||:||var_emp.name||:||var_emp.salary);

                /*將遊標的第一條值存儲到var_emp中*/
                fetch   empcursor  into  var_emp;
                dbms_output.put_line(var_emp.id||:||var_emp.name||:||var_emp.salary); 

                /*關閉遊標*/
                close   empcursor;  
         end;
         /

【PLSQL】PLSQL中復合數據類型