資料庫管理系統中的遊標
每當執行DML語句時,都會在系統記憶體中建立一個稱為遊標的臨時工作區。儘管遊標可能包含多行,但在處理方面,一次只考慮一行。遊標對Oracle、Mysql、SQL Server等型別的DBMS非常有幫助。遊標是用於遍歷資料庫記錄的控制結構,並用於資料庫中。
遊標分為兩種型別
隱式遊標
顯式遊標
隱式遊標
如果不對語句使用顯式遊標,則在執行SQL語句時,Oracle將建立隱式遊標。一些用於隱式遊標以檢查DML操作狀態的屬性是%ROWCOUNT、%ISOPEN、%FOUND和%NOTFOUND。
%ROWCOUNT
它顯示SELECT INTO命令產生的行數或受INSERT、DELETE和UPDATE等DML操作影響的行數。
%ISOPEN
鑑於相關的SQL語句執行完畢後,SQL遊標會自動關閉,因此它始終對隱式遊標返回FALSE。
%FOUND
如果SELECT INTO命令產生了一行或多行,或者如果INSERT、DELETE和UPDATE等DML操作影響了一行或多行,則其返回結果為TRUE。否則,返回FALSE。
%NOTFOUND
如果SELECT INTO命令產生了一行或多行,或者如果INSERT、DELETE和UPDATE等DML操作影響了一行或多行,則其返回結果為TRUE。否則,返回FALSE。
%NOTFOUND
如果INSERT、DELETE和UPDATE等DML操作沒有影響任何行,或者SELECT INTO命令沒有返回任何行,則其返回結果為TRUE。否則,返回FALSE。它與%FOUND正好相反。
示例
建立一個學生表並新增記錄。
演算法
步驟1:宣告要受影響的行總數。
步驟2:使用BEGIN
步驟3:將學生的成績更新為加5。
步驟4:使用if條件判斷%notfound
步驟5:列印未更新記錄的輸出。
步驟6:使用else if條件判斷%found
步驟7:宣告%rowcount的數量等於總行數。
步驟8:顯示總行數中已更新的行總數。
步驟9:關閉END
步驟10:顯示更新後的表
輸入
學生
Student ID First Name Last Name Address Marks 1 Rahul Raj Delhi 92 2 Saksham Pandey Lucknow 81
3 Amresh Rao Panipat 91 4 Sumit Patil Pune 61 5 Sneha Singh Kochi 75
使用SQL%ROWCOUNT屬性更新表併為每個學生的成績增加5分。
示例
DECLARE total_rows number(2); #Declaring the total number of rows to be affected BEGIN update students marks= marks + 5; #Updating students marks by 5 if sql%notfound then #Using if condition for %notfound dbms_output.put_line('Records not updated’); #Printing the result of %notfound elseif sql%found then #Using elseif for %found total_rows:= sql%rowcount; #Total rows will be equal to the total row count dbms_output.put_line(‘total rows updated: ’ || total_rows ); #Printing the result of total rows updated in total rows end if; END; #Closing the END SELECT * FROM Students;#to show the updated table
輸出
Student ID First Name Last Name Addres Marks 1 Rahul Raj Delhi 97 2 Saksham Pandey Lucknow 86 3 Amresh Rao Panipat 96 4 Sumit Patil Pune 66 5 Sneha Singh Kochi 80
顯式遊標
程式設計師定義顯式遊標是為了在上下文區域上擁有更大的控制範圍。它必須在PL/SQL塊的宣告部分定義。
語法
CURSOR cursor_name IS select_statement;
建立顯式遊標的步驟:
初始化遊標記憶體
CURSOR <遊標名稱> IS SELECT <所需欄位> FROM <表名>;
分配遊標記憶體
OPEN <遊標名稱>;
提取檢索資料
FETCH <遊標名稱> INTO <相應列>;
釋放已分配的記憶體
CLOSE <遊標名稱>;
示例
在這個例子中,我們將看到如何使用顯式遊標。
演算法
步驟1:宣告受影響的細節
步驟2:初始化記憶體
步驟3:使用Begin
步驟4:分配記憶體
步驟5:建立一個迴圈
步驟6:提取資料
步驟7:如果資料未找到則結束迴圈
步驟8:釋放已分配的記憶體
輸入
學生
Student ID Name Address Marks 1 Rahul Delhi 97 2 Saksham Lucknow 86 3 Amresh Panipat 96 4 Sumit Pune 66 5 Sneha Kochi 80
示例
DECLARE s_id student.id%type; #declaring the id s_name student.name%type; #declaring the name s_addr student.address%type; #declaring the address CURSOR s_student is SELECT id, name, address FROM student; #initialising the memory BEGIN OPEN s_student; #memory allocating LOOP #creating a loop FETCH s_student into s_id, s_name, s_addr; #fetching the data EXIT WHEN s_student%notfound; #condition if data not found END LOOP; #closing the loop
輸出
Student ID Name Address 1 Rahul Delhi 2 Saksham Lucknow 3 Amresh Panipat 4 Sumit Pune 5 Sneha Kochi
結論
本文介紹了在DBMS中使用的遊標。遊標分為兩種型別。第一種是隱式遊標,當我們不使用顯式遊標時建立。隱式遊標下使用%rowcount、%isopen、%found和%notfound等屬性。第二種是顯式遊標,它可以大規模控制上下文區域。建立顯式遊標的步驟是:初始化、分配記憶體、提取資料和釋放已分配的記憶體。