MyTetra Share
Делитесь знаниями!
Вывод location_country той же страны
Время создания: 05.11.2017 10:30
Раздел: PL/SQL - Задачник - CURSORS - Параметризованный курсор
Запись: xintrea/mytetra_db_mcold/master/base/15098670283nis7b9xcq/text.html на raw.githubusercontent.com

написать функцию выводящую сочетание записей локации + страна

при совпадении страны

в качестве аргумента используется номер локации


declare

loc_cur sys_refcursor;

n_loc_id integer;

c_id countries.country_id%TYPE;

cc_id countries.country_id%TYPE;

c_id_from_loc countries.country_id%TYPE;

c_name countries.country_name%TYPE;

rec_loc locations%ROWTYPE;

l_id locations.location_id%TYPE;

ll_id locations.location_id%TYPE;

cursor c_location(l_id locations.location_id%TYPE) is

select l.country_id

from locations l

where l.location_id = l_id;

cursor c_country(c_id countries.country_id%TYPE) is

select c.country_id

, c.country_name

from countries c

where c.country_id = c_id;

cursor c_country_all is

select c.country_id

, c.country_name

from countries c;

cursor c_loc_country(l_id locations.location_id%TYPE) is

select l.location_id

, l.city

, c.country_id

, c.country_name

from countries c

, locations l

where l.country_id = c.country_id

and l.location_id = l_id;

cursor c_loc_country_all is

select l.location_id

, l.city

, c.country_id

, c.country_name

from countries c

, locations l

where l.country_id = c.country_id;

-- and l.location_id = l_id;

rec_country_from_loc c_country%ROWTYPE;

rec_country c_country_all%ROWTYPE;

rec_loc_country_from_loc c_loc_country%ROWTYPE;

rec_loc_country c_loc_country_all%ROWTYPE;

begin

l_id := 2500; -- Oxford

-- получаем строку Оксфорда

open c_loc_country(l_id);

fetch c_loc_country into rec_loc_country_from_loc;

-- получаем строку для сравнения

c_id := rec_loc_country_from_loc.country_id;

open c_country(c_id);

fetch c_country into rec_country_from_loc;

dbms_output.put_line(rec_loc_country_from_loc.city

|| ' ' || rec_loc_country_from_loc.location_id

|| ' ' || rec_loc_country_from_loc.country_id

|| ' ' || rec_country_from_loc.country_name);

close c_country;

close c_loc_country;

-- обход всех локаций для определения одинаковых

for rec in c_loc_country_all loop

-- для каждой локации находим строку для сравнения

cc_id := rec.country_id;

open c_country(cc_id);

fetch c_country into rec_country;

close c_country;

-- сравнение строк

if rec_country.country_id = rec_country_from_loc.country_id

and rec_country.country_name = rec_country_from_loc.country_name

and not rec.city = rec_loc_country_from_loc.city

then

dbms_output.put_line(rec.city

|| ' ' || rec.location_id

|| ' ' || rec.country_id

|| ' ' || rec_country.country_name);

end if;

end loop;

end;


Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования