當前位置:成語大全網 - 新華字典 - 怎麽壹行壹行的遍歷數據庫中的所有行

怎麽壹行壹行的遍歷數據庫中的所有行

使用遊標可以輔助實現壹行壹行地遍歷某個數據庫表中的所有行。

例如在Oracle數據庫中:

declare

sname varchar2( 20); --聲明變量

cursor student_cursor is select sname from student ; --聲明指向表student的遊標

begin

open student_cursor;--打開遊標

fetch student_cursor into sname ;--獲取遊標的當前指向值,使遊標指針往下移動

while student_cursor%found --判斷遊標指針是否指向某行記錄,即是否已遍歷完全表

loop--循環遍歷

dbms_output.put_line ('學生姓名' ||sname );--輸出當前遍歷訪問的行記錄信息

fetch student_cursor into sname;--獲取遊標的當前指向值,使遊標指針往下移動

end loop;--循環體結束

close student_cursor;--關閉遊標,對應前面open操作

end;