Индивидуал элементларни бошқариш
Агар сиз коллекциянинг алоҳида элементини бошқармоқчи бўлсангиз TABLE операторидан фойдаланинг. TABLE операнди қисмсуров бўлиб, ягона устун қийматини бошқариш учун қайтарилади. Ушбу қиймат массив ёки ичма-ич жойлашган жадвалдир.
Қуйидаги мисолда courses устунида сақловчи History Бўлими ичма-ич жойлашган жадвалга қатор қўшади:
BEGIN
INSERT INTO
TABLE(SELECT courses FROM department WHERE name = 'History')
VALUES(3340, 'Modern China', 4);
END;
Қуйидаги мисолда алоҳида олинган коллекция элементини янгилаш намойиш этилган:
DECLARE
adjustment INTEGER DEFAULT 1;
BEGIN
UPDATE TABLE(SELECT courses FROM department
WHERE name = 'Psychology')
SET credits = credits + adjustment
WHERE course_no IN (2200, 3540);
END;
Қуйидаги мисол алоҳида коллекция элеметини сўров орқали олинади:
DECLARE
my_course_no NUMBER(4);
my_title VARCHAR2(35);
BEGIN
SELECT course_no, title INTO my_course_no, my_title
FROM TABLE(SELECT courses FROM department
WHERE name = 'History')
WHERE course_no = 3105;
...
END;
Коллекция алоҳида элементларини ўчириб ташлаш қуйидаги мисолда намойиш этилган:
BEGIN
DELETE TABLE(SELECT courses FROM department
WHERE name = 'English')
WHERE credits = 5;
END;
Массив устунидан алоҳида элементларни сўровлаш:
DECLARE
my_cost NUMBER(7,2);
my_title VARCHAR2(35);
BEGIN
SELECT cost, title INTO my_cost, my_title
FROM TABLE(SELECT projects FROM department
WHERE dept_id = 50)
WHERE project_no = 4;
...
END;
Айни пайтда массивнинг алоҳида элементларига INSERT, UPDATE, ёки DELETE жумлаларида мурожаат қилиб билмайсиз. statement. Шу мақсадда PL/SQL процедуравий жумлаларидан фойдаланишингиз лозим:
CREATE PROCEDURE add_project (
dept_no IN NUMBER,
new_project IN Project,
position IN NUMBER) AS
my_projects ProjectList;
BEGIN
SELECT projects INTO my_projects FROM department
WHERE dept_no = dept_id FOR UPDATE OF projects;
my_projects.EXTEND;--янги проект у-н жой очиш /* массив элементларини олдинга силжитиш*/
FOR i IN REVERSE position..my_projects.LAST - 1 LOOP
my_projects(i + 1) := my_projects(i);
END LOOP;
my_projects(position) := new_project; UPDATE department SET projects = my_projects
WHERE dept_no = dept_id;
END add_project;
Қуйидаги сақланадиган процедура жадвали қийматини ўзгартиради:
CREATE PROCEDURE update_project (
dept_no IN NUMBER,
proj_no IN NUMBER,
new_title IN VARCHAR2 DEFAULT NULL,
new_cost IN NUMBER DEFAULT NULL) AS
my_projects ProjectList;
BEGIN
SELECT projects INTO my_projects FROM department
WHERE dept_no = dept_id FOR UPDATE OF projects;
/* Проектни топиш, уни янгилаш, сўнг циклдан дарҳол чиқиб кетиш. */
FOR i IN my_projects.FIRST..my_projects.LAST LOOP
IF my_projects(i).project_no = proj_no THEN
IF new_title IS NOT NULL THEN
my_projects(i).title := new_title;
END IF;
IF new_cost IS NOT NULL THEN
my_projects(i).cost := new_cost;
END IF;
EXIT;
END IF;
END LOOP;
UPDATE department SET projects = my_projects
WHERE dept_no = dept_id;
END update_project;
Do'stlaringiz bilan baham: |