在本章中,我们将讨论和学习pl/sql中的记录。 记录是可以容纳不同种类的数据项的数据结构。 记录由不同的字段组成,类似于数据库表的一行。
例如,想要在图书馆中跟踪记录图书信息。可能希望跟踪每本书的以下属性,例如标题,作者,主题,图书id。 包含每个这些项目的字段的记录允许将图书视为逻辑单元,并允许以更好的方式组织和表示其信息。
pl/sql可以处理以下类型的记录 -
%rowtype
属性使程序员能够创建基于表和基于游标的记录。
以下示例说明了基于表的记录的概念。这里将使用前面章节中创建和使用的customers
表,表结构和数据如下 -
create table customers(
id int not null,
name varchar (20) not null,
age int not null,
address char (25),
salary decimal (18, 2),
primary key (id)
);
-- 插入示例数据
insert into customers (id,name,age,address,salary)
values (1, 'ramesh', 32, 'ahmedabad', 2000.00 );
insert into customers (id,name,age,address,salary)
values (2, 'khilan', 25, 'delhi', 1500.00 );
insert into customers (id,name,age,address,salary)
values (3, 'kaushik', 23, 'kota', 2000.00 );
insert into customers (id,name,age,address,salary)
values (4, 'chaitali', 25, 'mumbai', 6500.00 );
insert into customers (id,name,age,address,salary)
values (5, 'hardik', 27, 'bhopal', 8500.00 );
insert into customers (id,name,age,address,salary)
values (6, 'komal', 22, 'mp', 4500.00 );
使用表记录示例代码 -
set serveroutput on size 99999;
declare
customer_rec customers%rowtype;
begin
select * into customer_rec
from customers
where id = 5;
dbms_output.put_line('客户id: ' || customer_rec.id);
dbms_output.put_line('客户姓名: ' || customer_rec.name);
dbms_output.put_line('客户地址: ' || customer_rec.address);
dbms_output.put_line('客户薪资: ' || customer_rec.salary);
end;
/
当上述代码在sql提示符下执行时,它会产生以下结果 -
以下示例说明了基于游标的记录的概念,下面将使用在前面创建和使用的customers
表,参考示例代码如下 -
set serveroutput on size 99999;
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提示符下执行时,它会产生以下结果 -
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;
图书(book)记录按以下方式声明 -
declare
type books is record
(
title varchar(50),
author varchar(50),
subject varchar(100),
book_id number
);
book1 books;
book2 books;
访问字段
要访问记录的任何字段,可通过使用点(.
)运算符。成员访问操作符被编码为记录变量名称和希望访问的字段。看盾以下一个例子中如何使用记录 -
set serveroutput on size 99999;
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 := 'tanhao';
book1.subject := 'c programming tutorial';
book1.book_id := 1920122;
-- book 2 specification
book2.title := 'telecom billing';
book2.author := 'lidawei';
book2.subject := 'telecom billing tutorial';
book2.book_id := 2032942;
-- 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 : tanhao
book 1 subject : c programming tutorial
book 1 book_id : 1920122
book 2 title : telecom billing
book 2 author : lidawei
book 2 subject : telecom billing tutorial
book 2 book_id : 2032942
pl/sql 过程已成功完成。
可以像传递任何其他变量一样将记录作为子程序参数传递。还可以像访问上面的示例一样访问记录字段,参考下示例代码 -
set serveroutput on size 99999;
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 := 'haoqiang tang';
book1.subject := 'c programming tutorial';
book1.book_id := 8321407;
-- book 2 specification
book2.title := 'telecom billing';
book2.author := 'maxsu';
book2.subject := 'telecom billing tutorial';
book2.book_id := 921300;
-- use procedure to print book info
printbook(book1);
printbook(book2);
end;
/
当上述代码在sql提示符下执行时,它会产生以下结果 -
book title : c programming
book author : haoqiang tang
book subject : c programming tutorial
book book_id : 8321407
book title : telecom billing
book author : maxsu
book subject : telecom billing tutorial
book book_id : 921300
pl/sql 过程已成功完成。