oracle - pl/sql How to use for loop with cursor for update operation -
i'm new in pl/sql , on book found example update cursors
declare cursor c_grade(i_student_id in enrollment.student_id%type, i_section_id in enrollment.section_id%type ) select final_grade enrollment student_id_id = i_student_id , section_id = i_section_id update; cursor c_enrollment select e.student_id, e.section_id enrollment enr, section sec sec.course_no = 135 , enr.section_id = sec_section_id; begin r_enroll in c_enrollment loop r_grade in c_grade(r_enroll.student_id. r_enroll.section_id) loop update enrollment set final_grade = 90 student_id = r_enroll.student_id , section_id = r_enroll.section_id; end loop; end loop; end;
my question why need use update cursor in example? benefit compared this:
r_enroll in c_enrollment loop update enrollment set final_grade = 90 student_id = r_enroll.student_id , section_id = r_enroll.section_id; end loop;
as mentioned in comment, for update clause used lock rows within select statement such no other user able change these rows (update, delete, ... etc.) until lock release.
the lock typically released automatically once issue commit or rollback statements. particularly useful when there high number of dml statements running on same set of data different users. example, need update row highest id @ moment, before update row, row updated higher id different user.
for example, using
for r_grade in c_grade(r_enroll.student_id. r_enroll.section_id) loop
will guarantee updating version locked, , nobody else has updated before you.
you can find elaborated description example here
Comments
Post a Comment