Oracle PL/SQL複合資料型別,oraclepl

來源:互聯網
上載者:User

Oracle PL/SQL複合資料型別,oraclepl

   複合資料型別大致可以分為兩類。一類是記錄類型,適用於處理單行多列資料,有點類似於java中的VO;一類是集合類型,適用於處理單列多行的資料,類似java中的List,以下實驗在11.2.0.1.0版本下做的。

1.記錄類型

drop table test purge;

create table test
(
  id number(2),
  name varchar2(60)
);
insert into test values(1,'aaa');
insert into test values(2,'bbb');
insert into test values(3,'ccc');
insert into test values(4,'ddd');
insert into test values(5,'eee');

commit;

--顯式定義記錄類型
declare
type t_record is record
(
  id test.id%type,
  name test.name%type
);
var_record t_record;
coun number := 0;
begin
  for c_row in (select id,name from test) loop
    coun := coun + 1;
    dbms_output.put_line('第'||coun||'迴圈');
    var_record.id := c_row.id;
    var_record.name := c_row.name;
    dbms_output.put_line('記錄:'||var_record.id||'---'||var_record.name);
    dbms_output.put_line('遊標:'||c_row.id||'---'||c_row.name);
   end loop;
   exception when others then
          dbms_output.put_line(sqlcode||sqlerrm);
end;
/

輸出結果:
第1迴圈
記錄:1---aaa
遊標:1---aaa
第2迴圈
記錄:2---bbb
遊標:2---bbb
第3迴圈
記錄:3---ccc
遊標:3---ccc
第4迴圈
記錄:4---ddd
遊標:4---ddd
第5迴圈
記錄:5---eee
遊標:5---eee


--隱式定義記錄類型
declare
t_record1 test%rowtype;
cursor c_row(v_id in varchar2) is select id,name from test where id <= v_id;
t_record2 c_row%rowtype;
begin
    for row_test in c_row(3) loop
      t_record1.id := row_test.id;
      t_record1.name := row_test.name;
      t_record2.id := row_test.id;
      t_record2.name := row_test.name;
      dbms_output.put_line('表的rowtype:'||t_record1.id||'---'||t_record1.name);
      dbms_output.put_line('遊標的rowtype:'||t_record2.id||'---'||t_record2.name);
      dbms_output.put_line('遊標:'||row_test.id||'---'||row_test.name);
    end loop;
    exception when others then
            dbms_output.put_line(sqlcode||sqlerrm);
end;
/

輸出結果:

表的rowtype:1---aaa
遊標的rowtype:1---aaa
遊標:1---aaa
表的rowtype:2---bbb
遊標的rowtype:2---bbb
遊標:2---bbb
表的rowtype:3---ccc
遊標的rowtype:3---ccc
遊標:3---ccc

  如果在顯式和隱式定義記錄中選擇,我傾向於選擇顯式的定義,因為讓邏輯更加清晰。

2.集合類型

--索引表
declare
cursor cur_test is select id,name from test;
type t_test1 is table of test%rowtype index by binary_integer;
var_test1 t_test1;
begin
SELECT id,name INTO var_test1(0) FROM test WHERE id=1;
dbms_output.put_line('var_test1(0):'||var_test1(0).id||'---'||var_test1(0).name);
SELECT id,name INTO var_test1(10) FROM test WHERE id=2;
dbms_output.put_line('var_test1(10):'||var_test1(10).id||'---'||var_test1(10).name);
end;

var_test1(0):1---aaa
var_test1(10):2---bbb

--巢狀表格
DECLARE 
TYPE t_test1 IS TABLE OF test.id%TYPE;
var_test1 t_test1;
begin
        var_test1 := t_test1(1,2,3);
        dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
end;

var_test1: 1,2,3

--varray表
DECLARE 
TYPE t_test1 IS VARRAY (10) OF test.id%TYPE;
var_test1 t_test1;
begin
        var_test1 := t_test1(1,2,3);
        dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
end;
var_test1: 1,2,3

索引表和巢狀表格集合中的元素的數量沒有限制,varray集合中是沒有限制的。
索引表不能儲存在資料庫中,巢狀表格和varray可以被儲存在資料庫中。


oracle pl/sql中怎使用變數

定義並使用變數

PL/SQL有四種類型:標量類型,複合類型,參考型別 (reference),LOB(Large Obejct)類型

一、標量類型

最常用的就是標量類型,是指只能存放單個數值的變數,包括數字類型、字元類型、日期類型和布爾類型,每種類型又包含相應的子類型。

常量標量類型如下:

VARCHAR2 (n) , CHAR (n), NUMBER (p,s),DATE, TIMESTAMP , LONG , LONG RAW ,BOOLEAN,BINARY_INTEGER(僅 PL / SQL使用),BINARY_FLOAT和BINARY_DOUBLE(10g新引入的)

定義標量:

identifier [CONSTANT] datatype [NOT NULL] [:=| DEFAULT expr]

使用標量需要注意的是=號被:=取代,與delphi一樣的賦值符號@_@

例子:

v_name VARCHAR2 ( 10 );
v_rate CONSTANTS NUMBER ( 4 , 2 ) : = 3.04 ;

為了防止定義的變數類型與表中的欄位類型不一致,可以使用%TYPE來定義:

v_name employee.name % TYPE;

如上面所示,v_name的類型就與表 employee中的name欄位類型一樣!!

二、複合變數:

用於存放多個值的變數稱為複合變數,包括PL/SQL記錄,PL/SQL表,巢狀表格和VARRAY四種類型

1.PL/SQL記錄

類似於C/C++中的結構概念:

declare
TYPE employee_record is RECORD(
id employee.id % TYPE,
name employee.name % TYPE,
email employee.email % TYPE);
em_record employee_record;
begin
select id,name,email into em_record from employee where name =& name;
dbms_output.put_line( ' 僱員名: ' || em_record.name || ' 僱員ID: ' || em_record.id);
end ;

2.PL/SQL表,類似於數組概念,不同的是PL/SQL表允許負值下標,而且沒有上下限,如:

declare
TYPE employee_table is table of employee.name % TYPE index by BINaRY_INTEGER;
em_table employee_table;
begin
select name into em_table( - 1 ) from employee where name =& name;
dbms_output.put_line( ' 僱員名: ' || em_table( - 1 ));
end ;

3.巢狀表格,與PL/SQL 表相似,不同的是巢狀表格可以做表列的資料類型,而PL/SQL表不能,使用巢狀表格作為表列時,必須為其指定專門的儲存表,如:

create ......餘下全文>>
 
oracle資料庫我用PL/SQL建表有些欄位的標識我看不懂麻煩解釋一下!

binary_double 雙精確度64位
binary_float 雙精確度32位
blob,clob,nclob 三種大型物件(LOB),用來儲存較大的圖形檔案或帶格式的文字檔,如Miceosoft Word文檔,以及音頻、視頻等非文字檔,最大長度是4GB。LOB有幾種類型,取決於你使用的位元組的類型,Oracle 8i實實在在地將這些資料存放區在資料庫內部儲存。
可以執行讀取、儲存、寫入等特殊操作。
interval day to second,interval year to month 類型儲存兩個TIMESTAMP之間的時間差異,秒,月
long 可變長字元列,最大長度限制是2GB,用於不需要作字串搜尋的長串資料,如果要進行字元搜尋就要用varchar2類型。
timestamp 時間戳記,比date更精確
timestamp with time zone ,timestamp with local time zone 時間戳記(時區不一樣)
number 數字型

回答完畢
參考資料:dev.csdn.net/article/38/38823.shtm
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.