PL/SQL的記錄就是可以儲存不同類型的資料物件的資料結構。記錄由不同字段組成類似於資料庫表格的Row。
例如,要保留跟蹤圖書館中的書籍。可能要跟蹤有關每本書的屬性例如:標題,作者,主題,書籍ID。包含字段為每個這些項目記錄允許進行處理書籍作為一個邏輯資訊。
PL/SQL可以處理記錄的以下幾種類型:
- 表格記錄
- 遊標的記錄
- 使用者自定義記錄
表格的記錄
在%ROWTYPE屬性是建置表格和遊標記錄。
下面的例子將說明表的記錄的概念。(範例Table)
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; / |
結果如下:
Customer ID: 5
Customer Name: Hardik Customer Address: Bhopal Customer Salary: 9000 PL/SQL procedure successfully completed. |
遊標記錄
下面的例子將說明遊標記錄的概念。
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; / |
結果如下:
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; / |
結果如下:
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; / |
結果如下:
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. |
沒有留言:
張貼留言