PL/SQL記錄

PL/SQL記錄就是可以容納不同類型的數據項的數據結構。記錄由不同字段,類似於數據庫表的行。

例如,要保留跟蹤圖書館中的書籍。可能要跟蹤有關每本書下面的屬性類似:標題,作者,主題,圖書ID。包含一個字段爲每個這些項目記錄允許進行處理書籍作爲一個邏輯單元信息。

PL/SQL可以處理記錄的以下幾種類型:

  • 基於數據表

  • 基於遊標的記錄

  • 用戶自定義記錄

基於表的記錄

在%ROWTYPE屬性使程序員創建基於表格和基於遊標記錄。

下面的例子將說明基於表的記錄的概念。使用我們已經創建,並在前面的章節中使用的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.

基於遊標記錄

下面的例子將說明了基於遊標記錄的概念。使用我們已經創建,並在前面的章節中使用的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.

用戶自定義記錄

PL/SQL提供了一個用戶定義的記錄類型,允許定義不同的記錄結構。記錄由不同的字段組成。假設要跟蹤圖書館的書籍。可能要跟蹤有關每本書以下屬性:

  • 標題

  • 作者

  • 科目

  • 書籍ID

定義一條記錄

記錄類型定義爲:

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 TYPE books IS RECORD (title varchar(50), author varchar(50), subject varchar(100), book_id number); book1 books; book2 books;

訪問字段

要訪問記錄的字段,我們使用點(.)運算符。成員訪問運算符編碼爲記錄變量名和訪問字段期間。以下爲例子來解釋記錄的使用:

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.

記錄作爲子程序參數

可以通過記錄作爲子程序參數,非常相似傳遞任何其他變量的方式。訪問可以象在上面的例子 - 已訪問類似的方式記錄字段:

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.