Plsql 简明教程
PL/SQL - Records
在本章中,我们将讨论 PL/SQL 中的记录。 record 是可以容纳不同种类数据项目的数据结构。记录由不同的字段组成,类似于数据库表中的行。
例如,您希望跟踪库中的书籍。您可能想要跟踪每本书的以下属性,例如书名、作者、主题、书本编号。包含各个项目字段的记录可以将书本作为一个逻辑单元进行处理,并允许您更好地组织和呈现其信息。
PL/SQL 可以处理以下类型的记录:
-
Table-based
-
Cursor-based records
-
User-defined records
Table-Based Records
%ROWTYPE 属性使编程人员可以创建 table-based 和 cursorbased 记录。
以下示例说明了 table-based 记录的概念。我们将使用上几章中创建和使用的 CUSTOMERS 表:
DECLARE
customer_rec customers%rowtype;
BEGIN
SELECT * into customer_rec
FROM customers
WHERE id = 5;
dbms_output.put_line('Customer ID: ' || customer_rec.id);
dbms_output.put_line('Customer Name: ' || customer_rec.name);
dbms_output.put_line('Customer Address: ' || customer_rec.address);
dbms_output.put_line('Customer Salary: ' || customer_rec.salary);
END;
/
当以上代码在 SQL 提示符下执行时,它会生成以下结果:
Customer ID: 5
Customer Name: Hardik
Customer Address: Bhopal
Customer Salary: 9000
PL/SQL procedure successfully completed.
Cursor-Based Records
以下示例说明了 cursor-based 记录的概念。我们将使用上几章中创建和使用的 CUSTOMERS 表:
DECLARE
CURSOR customer_cur is
SELECT id, name, address
FROM customers;
customer_rec customer_cur%rowtype;
BEGIN
OPEN customer_cur;
LOOP
FETCH customer_cur into customer_rec;
EXIT WHEN customer_cur%notfound;
DBMS_OUTPUT.put_line(customer_rec.id || ' ' || customer_rec.name);
END LOOP;
END;
/
当以上代码在 SQL 提示符下执行时,它会生成以下结果:
1 Ramesh
2 Khilan
3 kaushik
4 Chaitali
5 Hardik
6 Komal
PL/SQL procedure successfully completed.
User-Defined Records
PL/SQL 提供了一个用户定义的记录类型,允许您定义不同的记录结构。这些记录由不同的字段组成。假设您希望跟踪库中的书籍。您可能想要跟踪每本书的以下属性:
-
Title
-
Author
-
Subject
-
Book ID
Defining a Record
记录类型定义为:
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;
Book 记录以以下方式声明:
DECLARE
TYPE books IS RECORD
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
Accessing Fields
要访问记录的任意字段,我们使用点 (.) 运算符。成员访问运算符被编码为记录变量名和我们希望访问的字段之间的句点。下面是一个解释记录用法的示例:
DECLARE
type books is record
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
BEGIN
-- Book 1 specification
book1.title := 'C Programming';
book1.author := 'Nuha Ali ';
book1.subject := 'C Programming Tutorial';
book1.book_id := 6495407;
-- Book 2 specification
book2.title := 'Telecom Billing';
book2.author := 'Zara Ali';
book2.subject := 'Telecom Billing Tutorial';
book2.book_id := 6495700;
-- Print book 1 record
dbms_output.put_line('Book 1 title : '|| book1.title);
dbms_output.put_line('Book 1 author : '|| book1.author);
dbms_output.put_line('Book 1 subject : '|| book1.subject);
dbms_output.put_line('Book 1 book_id : ' || book1.book_id);
-- Print book 2 record
dbms_output.put_line('Book 2 title : '|| book2.title);
dbms_output.put_line('Book 2 author : '|| book2.author);
dbms_output.put_line('Book 2 subject : '|| book2.subject);
dbms_output.put_line('Book 2 book_id : '|| book2.book_id);
END;
/
当以上代码在 SQL 提示符下执行时,它会生成以下结果:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
PL/SQL procedure successfully completed.
Records as Subprogram Parameters
您可以像传递任何其他变量一样将记录作为子程序参数进行传递。您还可以像在上述示例中访问记录字段一样访问记录字段:
DECLARE
type books is record
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
PROCEDURE printbook (book books) IS
BEGIN
dbms_output.put_line ('Book title : ' || book.title);
dbms_output.put_line('Book author : ' || book.author);
dbms_output.put_line( 'Book subject : ' || book.subject);
dbms_output.put_line( 'Book book_id : ' || book.book_id);
END;
BEGIN
-- Book 1 specification
book1.title := 'C Programming';
book1.author := 'Nuha Ali ';
book1.subject := 'C Programming Tutorial';
book1.book_id := 6495407;
-- Book 2 specification
book2.title := 'Telecom Billing';
book2.author := 'Zara Ali';
book2.subject := 'Telecom Billing Tutorial';
book2.book_id := 6495700;
-- Use procedure to print book info
printbook(book1);
printbook(book2);
END;
/
当以上代码在 SQL 提示符下执行时,它会生成以下结果:
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
PL/SQL procedure successfully completed.