plsql programming 11 记录类型

记录类型非常类似数据库表中的行. 记录作为一个整体本身并没有值, 不过每个单独成员或字段都有值, 记录提供了一种把这些值当做一组进行操作的方法.

例如:

   1:  -- create a table
   2:  -- chap11_01.sql
   3:  create table books (
   4:      book_id    integer;
   5:      isbn    varchar2(13),
   6:      title    varchar2(200),
   7:      summary    varchar2(2000),
   8:      author    varchar2(200),
   9:      date_published    DATE,
  10:      page_count    number
  11:  );
  12:   
  13:  -- 方法1
  14:  -- chap11_02.sql
  15:  declare
  16:      my_book    books%rowtype;
  17:  begin
  18:      select *
  19:        into my_book
  20:        from books
  21:       where title = 'oracle pl/sql programming, 5th Edition';
  22:       
  23:      if my_book.author like '%Feuerstein%'
  24:      then
  25:          dbms_output.put_line('Our newest ISBN is' || my_book.isbn);
  26:      end if;
  27:  exception
  28:      when no_data_found then
  29:          dbms_output.put_line('There is no data what you want it.');
  30:  end;
  31:  /
  32:   
  33:  -- 方法2
  34:  -- chap11_03.sql
  35:  declare
  36:      type author_title_rt is record (
  37:          author books.author%type,
  38:          title books.title%type);
  39:      l_book_info author_title_rt;
  40:  begin
  41:      select author, title into l_book_info
  42:          from books where isbn = '119'
  43:      dbms_output.put_line('author is : ' || l_book_info.author);
  44:      dbms_output.put_line('title : ' || l_book_info.title);
  45:  exception
  46:      when no_data_found then
  47:          dbms_output.putline('there is no data what you want');
  48:  end;
  49:  /

声明记录 3 种方式

1. 基于记录类型

  declare one_book books%rowtype;

2. 基于游标记录类型

  declare

     cursor my_books_cur is

        select * from books where author like ‘%FEUERSTEIN%’;

     one_sf_book my_books_cur%rowtype;

3. 用户自定义的记录类型

    例如上例中的方法 2 .

触发器伪记录

如果是在数据库触发器操作某个表, 数据库为我们提供了两种结构, OLD 和 NEW, 这两个结构是用 %rowtype声明的基于表的记录类型有相同的格式, 表中的每一列都有一个对应的字段:

OLD: 这条伪记录代表的是当前事务开始之前表中记录的值.

NEW: 这条伪记录代表的是当前事务结束之后, 表中记录的新值.

原文地址:https://www.cnblogs.com/moveofgod/p/3550475.html