PLSQL 7 4 Practice
PLSQL 7 4 Practice
com/academy
Try It / Solve It
1. Enter and run the following code twice, once for each of the two country_ids, 5 (which does
not exist) and 672 (Antarctica, which does exist but has no currency).
DECLARE
v_country_name countries.country_name%TYPE; v_currency_code
countries.currency_code%TYPE;
BEGIN
DECLARE
e_no_currency EXCEPTION;
BEGIN
SELECT country_name, currency_code INTO v_country_name, v_currency_code
FROM countries
WHERE country_id = 5; -- repeat with 672
IF v_currency_code = 'NONE' THEN
RAISE e_no_currency;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('This country does not exist');
WHEN e_no_currency THEN
DBMS_OUTPUT.PUT_LINE('This country exists but has no currency');
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Another type of error occurred');
END;
A. Explain the output. Save your code.
Country_id 5 aumenta NO_DATA_FOUND y se muestra 'Este país no existe'.
Country_id 672 aumenta e_no_currency y se muestra 'Este país existe pero no tiene
moneda'.
B. Modify the code to move the two exception handlers to the outer block. Leave the
declaration of e_no_currency in the inner block. Execute twice, again using country_ids 5
and 672. Now what happens and why? Save your code.
DECLARE
v_pais_nom countries.country_name%TYPE;
v_curr countries.currency_code%TYPE;
BEGIN
DECLARE
e_no_curr EXCEPTION;
BEGIN
SELECT country_name, currency_code
INTO v_pais_nom, v_curr
FROM countries
WHERE country_id = 5; - - Hacemos lo mismo pero con 672
IF v_curr = 'Ninguno' THEN
RAISE e_no_curr;
END IF;
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Este pais no existe.');
WHEN e_no_curr THEN
DBMS_OUTPUT.PUT_LINE('Este pais existe pero no tiene moneda.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Otro tipo de error ocurrio.');
END;