在本章中,我們將討論和學習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 過程已成功完成。