PLSQL 6 3 Practice PDF

November 9, 2022 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download PLSQL 6 3 Practice PDF...

Description

 

www.oracle.com/academy   www.oracle.com/academy 

 

Database Programming with PL/SQL 6-3: Cursor FOR Loops Practice  Activities Vocabulary Identify the vocabulary word for each definition below:

CURSORS FOR LOOP

 Automates standard  Automates standard cursor-han cursor-handling dling ope operations rations su such ch as OPE OPEN, N, FETCH, %NOTFOUND, and CLOSE so that they do not need to be coded explicitly

Try It / Solve It

1. Describe two benefits of using a cursor FOR loop. Se simplifi ca el - Se el codi go -simplifi ca la escritur a del código

2. Modify the following PL/SQL block so that it uses a cursor FOR loop. Keep the explicit cursor declaration in the DECLARE section. Test your changes. DECLARE CURSOR SELECT countries_cur country_name,ISnational_holiday_name, national_holiday_date FROM countries WHERE region_id = 5; countries_rec countries_cur%ROWTYPE; BEGIN OPEN countries_cur; LOOP FETCH countries_cur INTO countries_rec; EXIT WHEN countries_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE ('Country: ' || countries_rec.country_name || ' National holiday: '|| countries_rec.national_holiday_name || ', held on: '|| countries_rec.national_holiday_date); END LOOP; CLOSE countries_cur; END;

 

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks trademarks of Oracle and/ or its affiliates. Other names may be t rademarks of their respective owners.

2

DECLARE CURS CU RSOR OR countr ies_cur IS SELEC SELECT T coun try_name, national_hol natio nal_hol iday_name, national_holiday_date FROM FR OM countries count ries WH WHER ERE E region _id = 5; BEGIN FOR FO R v_countries IN countries_cur LOOP

DBMS_OUT DBMS_O UTPU PUT. T.PU PUT_LI T_LINE NE ('Count ('Count ry: ' || v_coun tri es.coun es.country_name try_name || ' National Na tional h oliday: '|| v _countries.national_holiday_ _countries.national_holiday_name name || ', held held on: '|| v_countries.national_holiday_date); END LOOP; END;   3. Modify your answer to question 2 to declare the cursor using a subquery in the FOR…LOOP statement, rather than in the declaration section. Test your changes again.

BEGIN FOR FO R v_countries v_count ries IN (SELEC (SELECT T count ry_name, national_holi nation al_holiday_name, day_name, national_holiday_date FROM FR OM countri coun tries es WHERE WHERE region_id = 5) LOOP DBMS_O DBMS _OUT UTPU PUT. T.PUT PUT_LIN _LINE E ('Countr y: ' || v_cou v_countr ntries.cou ies.countr ntr y_name || ' National holid ay: '|| v_countri es.na es.national_holiday_name tional_holiday_name || ', held held o n: ' || v_countries.national_holiday_date); END LOOP; END;   4. Using the COUNTRIES table, write a cursor that returns countries with a highest_elevation greater than 8,000 m. For each country, display the country_name, highest_elevation, and climate. Use a cursor FOR loop, declaring the cursor using a subquery in the FOR…LOOP statement.

BEGIN FOR v_countries IN (SELECT country_name, highest_elevation, climate FROM countries WHERE highest_elevation > 8000) LOOP DBMS_O DBMS _OUT UTPU PUT. T.PU PUT_LIN T_LINE E ('Countr y: ' || v_co unt untries.co ries.count untry_name ry_name || ' Highest Elevation: Ele vation: ' || v_count ries.highest_e ries.highest_eleva levation tion || ' ' || 'Climate: ' || v_countries.climate); END LOOP; END;  

5. This question uses a join of the SPOKEN_LANGUAGES and COUNTRIES tables with a GROUP BY and HAVING clause.

 

Write a PL/SQL block to fetch and display all the countries that have more than six spoken languages. For each such country, display country_name and the number of spoken languages. Use a cursor FOR loop, but declare the cursor explicitly in the DECLARE section. After all the rows have been fetched and displayed, display an extra row showing the total number of countries having more than six languages. (Hint: Declare a variable to hold the value of %ROWCOUNT.)

DECLARE CURSOR idiomas_cur IS SELECT country_name, COUNT(*) AS cant FROM FR OM countries c , sp oken_la oken_languages nguages l WHER WH ERE E c.countr y_id = l.coun try_id GROU GR OUP P BY cou ntr y_name HAVING COUNT(*) COUNT(*) > 6; v_to_pais PLS_INTEGER; BEGIN FOR FO R v_countri es IN idiom as_ as_cur cur LOOP DBMS_O DBMS _OUT UTPU PUT. T.PU PUT_LI T_LINE NE (v_cou (v_cou ntr ntries.cou ies.countr ntr y_name || ' habla ' || v_countries.cant || ' idiomas'); v_to_pais := idio i diomas_cur mas_cur%R %ROWC OWCOU OUNT NT;; END LOOP; DBMS_OUTPUT.PUT_LINE ('Total: ' || v_to_pais); END;  

6. Why did your block in question 4 need to declare the cursor explicitly, instead of declaring it as a subquery in the FOR…LOOP statement? Por q ue se necesit a acceder a un atrib uto %R %ROWC OWCOUN OUNT. T.

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks trademarks of Oracle and/ or its affiliates. Other names may be t rademarks of their respective owners.

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF