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;


猜你喜欢

转载自blog.csdn.net/henni_719/article/details/79753496