1. 程式人生 > >PL/SQL使用者自定義記錄(record)操作例項講解

PL/SQL使用者自定義記錄(record)操作例項講解

使用者自定義記錄

PL/SQL提供了一個使用者定義的記錄型別,允許定義不同的記錄結構。記錄由不同的欄位組成。

記錄型別定義為

TYPE
type_name IS RECORD
  ( field_name1  datatype1  [NOT NULL]  [:= DEFAULT EXPRESSION],
   field_name2   datatype2   [NOT NULL]  [:= DEFAULT EXPRESSION],
   ...
   field_nameN  datatypeN  [NOT NULL]  [:= DEFAULT EXPRESSION);
record-name  type_name;

使用者自定義記錄操作操作例項原始碼

-- Created on 2018/3/30 by E.WANG 
/*
PL/SQL提供了一個使用者定義的記錄型別,允許定義不同的記錄結構。
記錄由不同的欄位組成。
記錄型別定義為:
TYPE
type_name IS RECORD
  ( field_name1  datatype1  [NOT NULL]  [:= DEFAULT EXPRESSION],
   field_name2   datatype2   [NOT NULL]  [:= DEFAULT EXPRESSION],
   ...
   field_nameN  datatypeN  [NOT NULL]  [:= DEFAULT EXPRESSION);
record-name  type_name;
要訪問記錄的欄位,使用點(.)運算子。
成員訪問運算子編碼為記錄變數名和訪問欄位期間。
*/

declare 
  --宣告一條專輯資訊的記錄
  --專輯包括如下屬性:專輯名、作者、專輯id,專輯介紹、建立日期、聲音條數、專輯播放次數
  type album is record(
  title varchar(50),--專輯名
  author varchar(50) not null default 'ewang',--專輯作者
  albumId number(7),--專輯id
  info clob,--專輯介紹
  createDate date:=to_date('2018-03-30','YYYY-MM-DD'),--專輯建立時間
  trackCount int:=0,--專輯內聲音條數
  playCount  integer:=0 --專輯播放次數
  );
  --宣告福爾摩斯專輯
  album_Holmes album;
  --宣告傲慢與偏見
  album_prideAndPrejudice album;
begin
  --給福爾摩斯專輯賦值
  album_Holmes.title:='Sherlock Holmes';
  album_Holmes.author:='Conan Doyle';
  album_Holmes.albumId:=7007674;
  album_Holmes.trackCount:=321;
  album_Holmes.playCount:=63000;
  album_Holmes.createDate:=to_date('2018-03-08','YYYY-MM-DD');
  album_Holmes.info:='Sherlock Holmes (/ˈʃɜːrlɒk ˈhoʊmz/) is a fictional private detective created by British author Sir Arthur Conan Doyle.
   Known as a "consulting detective" in the stories, Holmes is known for a proficiency with observation, 
   forensic science, and logical reasoning that borders on the fantastic, 
   which he employs when investigating cases for a wide variety of clients, including Scotland Yard.
   First appearing in print in 1887 (in A Study in Scarlet), 
   the character''s popularity became widespread with the first series of short stories in The Strand Magazine, 
   beginning with "A Scandal in Bohemia" in 1891; 
   additional tales appeared from then to 1927, eventually totalling four novels and 56 short stories. 
   All but one are set in the Victorian or Edwardian periods,taking place between about 1880 to 1914. 
   Most are narrated by the character of Holmes''s friend and biographer Dr. Watson, 
   who usually accompanies Holmes during his investigations and often shares quarters with him at the address of 221B Baker Street, London, where many of the stories begin.
  ';
   
  --給傲慢與偏見賦值
  album_prideAndPrejudice.title:='Pride and Prejudice';
  album_prideAndPrejudice.author:='Jane Austen';
  album_prideAndPrejudice.albumId:=6852043;
  album_prideAndPrejudice.trackCount:=61;
  album_prideAndPrejudice.playCount:=3814;
  album_prideAndPrejudice.createDate:=to_date('2018-01-01','YYYY-MM-DD');
  album_prideAndPrejudice.info:='Pride and Prejudice is a novel by Jane Austen, first published in 1813. 
  The story charts the emotional development of the protagonist, Elizabeth Bennet,
   who learns the error of making hasty judgements and comes to appreciate the difference between the superficial and the essential.
   The comedy of the writing lies in the depiction of manners, education, and marriage and money in the British Regency.';
   
 --輸出album_Holmes記錄資訊
 dbms_output.put_line('album_Holmes title : '|| album_Holmes.title);
 dbms_output.put_line('album_Holmes author : '|| album_Holmes.author);
 dbms_output.put_line('album_Holmes albumId : '|| album_Holmes.albumId);
 dbms_output.put_line('album_Holmes trackCount : '|| album_Holmes.trackCount);
 dbms_output.put_line('album_Holmes playCount : ' || album_Holmes.playCount);
 dbms_output.put_line('album_Holmes createDate : '|| album_Holmes.createDate);
 dbms_output.put_line('album_Holmes info : ' || album_Holmes.info);
 

 --輸出album_prideAndPrejudice記錄資訊
 dbms_output.put_line('album_prideAndPrejudice title : '|| album_prideAndPrejudice.title);
 dbms_output.put_line('album_prideAndPrejudice author : '|| album_prideAndPrejudice.author);
 dbms_output.put_line('album_prideAndPrejudice albumId : '|| album_prideAndPrejudice.albumId);
 dbms_output.put_line('album_prideAndPrejudice trackCount : '|| album_prideAndPrejudice.trackCount);
 dbms_output.put_line('album_prideAndPrejudice playCount : ' || album_prideAndPrejudice.playCount);
 dbms_output.put_line('album_prideAndPrejudice createDate : '|| album_prideAndPrejudice.createDate);
 dbms_output.put_line('album_prideAndPrejudice info : ' || album_prideAndPrejudice.info);
  
end;

原始碼視窗截圖:


執行結果截圖:


記錄作為子程式引數

可以通過記錄作為子程式引數,非常相似傳遞任何其他變數的方式。視窗截圖和執行結果截圖 在此就不做贅述,原始碼如下:

-- Created on 2018/3/30 by E.WANG 
/*
PL/SQL提供了一個使用者定義的記錄型別,允許定義不同的記錄結構。
記錄由不同的欄位組成。
記錄型別定義為:
TYPE
type_name IS RECORD
  ( field_name1  datatype1  [NOT NULL]  [:= DEFAULT EXPRESSION],
   field_name2   datatype2   [NOT NULL]  [:= DEFAULT EXPRESSION],
   ...
   field_nameN  datatypeN  [NOT NULL]  [:= DEFAULT EXPRESSION);
record-name  type_name;
要訪問記錄的欄位,使用點(.)運算子。
成員訪問運算子編碼為記錄變數名和訪問欄位期間。
*/

declare 
  --宣告一條專輯資訊的記錄
  --專輯包括如下屬性:專輯名、作者、專輯id,專輯介紹、建立日期、聲音條數、專輯播放次數
  type album is record(
  title varchar(50),--專輯名
  author varchar(50) not null default 'ewang',--專輯作者
  albumId number(7),--專輯id
  info clob,--專輯介紹
  createDate date:=to_date('2018-03-30','YYYY-MM-DD'),--專輯建立時間
  trackCount int:=0,--專輯內聲音條數
  playCount  integer:=0 --專輯播放次數
  );
  --宣告福爾摩斯專輯
  album_Holmes album;
  --宣告傲慢與偏見
  album_prideAndPrejudice album;
  --定義以記錄型別作為引數的程序
  procedure printAlbumDetail(album_Holmes album)
  is
  begin
   dbms_output.put_line('album title : '|| album_Holmes.title);
   dbms_output.put_line('album author : '|| album_Holmes.author);
   dbms_output.put_line('album albumId : '|| album_Holmes.albumId);
   dbms_output.put_line('album trackCount : '|| album_Holmes.trackCount);
   dbms_output.put_line('album playCount : ' || album_Holmes.playCount);
   dbms_output.put_line('album createDate : '|| album_Holmes.createDate);
   dbms_output.put_line('album info : ' || album_Holmes.info);
  end;
  
 
begin
  --給福爾摩斯專輯賦值
  album_Holmes.title:='Sherlock Holmes';
  album_Holmes.author:='Conan Doyle';
  album_Holmes.albumId:=7007674;
  album_Holmes.trackCount:=321;
  album_Holmes.playCount:=63000;
  album_Holmes.createDate:=to_date('2018-03-08','YYYY-MM-DD');
  album_Holmes.info:='Sherlock Holmes (/ˈʃɜːrlɒk ˈhoʊmz/) is a fictional private detective created by British author Sir Arthur Conan Doyle.
   Known as a "consulting detective" in the stories, Holmes is known for a proficiency with observation, 
   forensic science, and logical reasoning that borders on the fantastic, 
   which he employs when investigating cases for a wide variety of clients, including Scotland Yard.
   First appearing in print in 1887 (in A Study in Scarlet), 
   the character''s popularity became widespread with the first series of short stories in The Strand Magazine, 
   beginning with "A Scandal in Bohemia" in 1891; 
   additional tales appeared from then to 1927, eventually totalling four novels and 56 short stories. 
   All but one are set in the Victorian or Edwardian periods,taking place between about 1880 to 1914. 
   Most are narrated by the character of Holmes''s friend and biographer Dr. Watson, 
   who usually accompanies Holmes during his investigations and often shares quarters with him at the address of 221B Baker Street, London, where many of the stories begin.
  ';
   
  --給傲慢與偏見賦值
  
  album_prideAndPrejudice.title:='Pride and Prejudice';
  album_prideAndPrejudice.author:='Jane Austen';
  album_prideAndPrejudice.albumId:=6852043;
  album_prideAndPrejudice.trackCount:=61;
  album_prideAndPrejudice.playCount:=3814;
  album_prideAndPrejudice.createDate:=to_date('2018-01-01','YYYY-MM-DD');
  album_prideAndPrejudice.info:='Pride and Prejudice is a novel by Jane Austen, first published in 1813. 
  The story charts the emotional development of the protagonist, Elizabeth Bennet,
   who learns the error of making hasty judgements and comes to appreciate the difference between the superficial and the essential.
   The comedy of the writing lies in the depiction of manners, education, and marriage and money in the British Regency.';
   
 --呼叫儲存過程輸出album_Holmes記錄資訊
 printAlbumDetail(album_Holmes);
 --輸出album_prideAndPrejudice記錄資訊
 printAlbumDetail(album_prideAndPrejudice);
 /*
 dbms_output.put_line('album_Holmes title : '|| album_Holmes.title);
 dbms_output.put_line('album_Holmes author : '|| album_Holmes.author);
 dbms_output.put_line('album_Holmes albumId : '|| album_Holmes.albumId);
 dbms_output.put_line('album_Holmes trackCount : '|| album_Holmes.trackCount);
 dbms_output.put_line('album_Holmes playCount : ' || album_Holmes.playCount);
 dbms_output.put_line('album_Holmes createDate : '|| album_Holmes.createDate);
 dbms_output.put_line('album_Holmes info : ' || album_Holmes.info);
 

 --輸出album_prideAndPrejudice記錄資訊
 dbms_output.put_line('album_prideAndPrejudice title : '|| album_prideAndPrejudice.title);
 dbms_output.put_line('album_prideAndPrejudice author : '|| album_prideAndPrejudice.author);
 dbms_output.put_line('album_prideAndPrejudice albumId : '|| album_prideAndPrejudice.albumId);
 dbms_output.put_line('album_prideAndPrejudice trackCount : '|| album_prideAndPrejudice.trackCount);
 dbms_output.put_line('album_prideAndPrejudice playCount : ' || album_prideAndPrejudice.playCount);
 dbms_output.put_line('album_prideAndPrejudice createDate : '|| album_prideAndPrejudice.createDate);
 dbms_output.put_line('album_prideAndPrejudice info : ' || album_prideAndPrejudice.info);
 */
end;