DBMS_OUTPUT.put_line(v_empno ||' '|| v_name);
END LOOP;
CLOSE empcur;
–––4: kursorni yopish
END;
Misol–2
Declare
CURSOR cur(gr NUMBER :=20) IS
SELECT * FROM student1 WHERE id_group = gr; i cur%ROWTYPE; BEGIN
OPEN cur(2);
LOOP
FETCH cur INTO i;
EXIT WHEN cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(i.name||chr(9)||i.id_group);
END LOOP;
CLOSE students;
END;
Natija:
Axmedov 2 Azimov 2
Misol–3. Guruh nomiga ko‘ra guruh talabalari haqidagi ma’lumotlarni aniqlovchi kursor yarating.
Bu misolda guruh va talabalar jadvalidan ma’lumotlarni olish talab etilmoqda. Ikkala jadvaldan ma’lumot olib kursor yaratish uchun avval ushbu ikktala jadvaldan ma’lumotlarni o‘qib oluvchi ko‘rinish (view) yaratishimiz zarur. Bu bizga natijada bitta virtual jadval asosida kursor yaratish imkonini beradi.
CREATE VIEW v_student AS
SELECT student1.name, groups.name AS gr_name
FROM student1, groups
WHERE student1.id_group = groups.id_group
SELECT * FROM v_student
Natija:
Kursorni yaratish:
DECLARE
Cursor students(gr VARCHAR2) IS
SELECT * FROM v_student WHERE gr_name = gr; i students%ROWTYPE;
––kursorning har bir satri uchun %ROW tipidan foydalanildi
BEGIN
OPEN students('AT–30');
LOOP
FETCH students into i;
EXIT WHEN students%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(i.name||chr(9)||i.gr_name);
END LOOP;
CLOSE students;
END;
Kursorni FOR operatori bilan qo‘llash:
Declare
CURSOR cur2(gr NUMBER :=2) IS
SELECT * FROM student1 WHERE id_group = gr;
BEGIN
FOR i IN cur2(2) LOOP
DBMS_OUTPUT.PUT_LINE(i.name||chr(9)||i.id_group);
END LOOP;
END;
BEGIN
FOR i IN (SELECT * FROM student1 WHERE id_group = '2')
LOOP
DBMS_OUTPUT.PUT_LINE(i.name||chr(9)||i.id_group);
END LOOP;
END;
Dasturlashda qism dastur tushunchasi mavjud bo‘lib, u dasturning bir qismini bajarilishini o‘z zimmasiga oladi. O‘z navbatida bitta katta dastur ana shunday qism dasturlardan tashkil topishi mumkin. Qism dasturlar dastur ishlash jarayonida murojaat qilinishi natijasida faollashadi, biror qiymatlarni qabul qilishi va qayta ishlab, keyingi qism dasturga uzatishi mumkin. PL/SQL da qism dasturlar ikki xil bo‘ladi: protsedura va funksiya.
Do'stlaringiz bilan baham: |