Explicit cursors for oracle
Declare cursor user_cur is select * from my_user; user_row my_user%rowtype; begin open user_cur; loop fetch user_cur into user_row; exit when user_cur%notfound; dbms_output.put_line (user_row.user_id | |'-'| | user_row.name); end loop; close user_cur; end;declare cursor user_cur is select * from my_user; row_user my_user%rowtype Begin open user_cur; loop fetch user_cur into row_user; exit when user_cur%notfound; dbms_output.put_line (row_user.user_id | |'-'| | row_user.name | |'-'| | row_user.age); end loop; close user_cur;end;declare cursor row_user is select * from my_user; type my_user_tab is table of my_user%rowtype / * define a collection type cur_row_user that is consistent with the table my_user row object, which is used to store data obtained in batches * / cur_row_user my_user_tab; begin open row_user; loop / * extract data from the result set, two rows at a time * / fetch row_user bulk collect into cur_row_user limit 2 / * traverse the data in the collection cur_row_user * / for i in 1..cur_row_user.count loop dbms_output.put_line (cur_row_user (I) .user_id | |'-'| | cur_row_user (I) .name | | cur_row_user (I) .age); end loop; exit when row_user%notfound; end loop Close row_user; end;declare cursor user_cur is select * from my_user; type my_user_tab is table of my_user%rowtype; / * defines a collection type cur_user_cur that is consistent with the table my_user row object, which is used to store batch data * / cur_user_cur my_user_tab; begin open user_cur Loop / * extract data from the result set, two rows at a time * / fetch user_cur bulk collect into cur_user_cur limit 2 / * traverse the data in the collection cur_user_cur * / for i in 1..cur_user_cur.count loop dbms_output.put_line (cur_user_cur (I) .user_id | |'-'| | cur_user_cur (I) .name | | cur_user_cur (I) .age); end loop; exit when user_cur%notfound; end loop Close user_cur; end;declare cursor user_cur is select * from my_user; begin for cdr in user_cur loop dbms_output.put_line (cdr.user_id | |'-'| | cdr.name | |'-'| | cdr.age); end loop; end / * cursor for loop does not need special declaration variables, it can extract travel object type data * / declare cursor user_cur is select * from my_user; cdr my_user%rowtype; begin if user_cur%isopen then fetch user_cur into cdr; dbms_output.put_line (cdr.user_id | |'-'| | cdr.name | |'-'| | cdr.age) Else dbms_output.put_line ('cursor not opened'); end if; end;declare cursor user_cur is select * from my_user; cdr my_user%rowtype; begin open user_cur; if user_cur%isopen then loop fetch user_cur into cdr; exit when user_cur%notfound Dbms_output.put_line (cdr.user_id | |'-'| | cdr.name | |'-'| | cdr.age); end loop; else dbms_output.put_line ('cursor is not opened'); end if; end;declare cursor user_cur is select * from my_user; cdr my_user%rowtype; begin open user_cur; loop fetch user_cur into cdr If user_cur%found then dbms_output.put_line (cdr.user_id | |'-'| | cdr.name | |'-'| | cdr.age); else dbms_output.put_line ('cursor is not open'); exit; end if; end loop; end Declare / * the values here can be written in both declare and begin * / v_user_id my_user.user_id%type:='&v_user_id'; / * the type of v_user_id here can be written as number and my_user.user_id%type can be * / cursor c_my_user (v_user_id my_user.user_id%type) is select * from my_user where user_id=v_user_id; cdr my_user%rowtype Begin open c_my_user (v_user_id); loop fetch c_my_user into cdr; if c_my_user%found then dbms_output.put_line (cdr.user_id | |'-'| | cdr.name | |'-'| | cdr.age); else dbms_output.put_line ('cursor not opened'); exit End if; end loop; end