Você está na página 1de 20

Session 4

Lectia1

5. Write a PL/SQL block to find the population of a given country in the wf_countries table. Display a message indicating whether the population is greater than or less than 1 billion (1,000,000,000). Test your block twice using India (country_id=91) and United Kingdom (country_id=44). Indias population should be greater than 1 billion, while United Kingdoms should be less than 1 billion.

DECLARE v_india wf_countries.population%TYPE ; v_nume wf_countries.COUNTRY_NAME%TYPE; BEGIN SELECT population,COUNTRY_NAME INTO v_india,v_nume FROM wf_countries WHERE country_id=91; IF v_india>1000000000 THEN DBMS_OUTPUT.PUT_LINE('populatia mai mare de 1.000.000.000 este ' ||v_nume); END IF; END;
populatia mai mare de 1.000.000.000 este Republic of India Statement processed.

Session 4

DECLARE v_india wf_countries.population%TYPE ; v_nume wf_countries.COUNTRY_NAME%TYPE; BEGIN SELECT population,COUNTRY_NAME INTO v_india,v_nume FROM wf_countries WHERE country_id=44; IF v_india<1000000000 THEN DBMS_OUTPUT.PUT_LINE('populatia mai mica de 1.000.000.000 este ' ||v_nume); END IF; END;
populatia mai mica de 1.000.000.000 este United Kingdom of Great Britain and Northern Ireland Statement processed.

6. Modify the code from the previous exercise so that it handles all the following cases:

A. Population is greater than 1 billion. B. Population is greater than 0. C. Population is 0. D. Population is null. (Display: No data for this country.) Run your script using the following countries: China (country_id=86): Population is greater than 1 billion. United Kingdom (country_id=44): Population is greater than 0. Antarctica (country_id=672): Population is 0. Europa Island (country_id=15): There is no data for this country.

Session 4

A.

DECLARE v_populatie wf_countries.population%TYPE; v_nume wf_countries.country_name%TYPE; BEGIN SELECT population,country_name INTO v_populatie,v_nume FROM wf_countries WHERE country_id=86; IF v_populatie>1000000000 THEN DBMS_OUTPUT.PUT_LINE('tara cu papulatia >1bilion este:' || v_nume); END IF; END;
tara cu papulatia >1bilion este:Peoples Republic of China Statement processed.

Session 4

B. DECLARE v_populatie wf_countries.population%TYPE; v_nume wf_countries.country_name%TYPE; BEGIN SELECT population,country_name INTO v_populatie,v_nume FROM wf_countries WHERE country_id=44; IF v_populatie>0 THEN DBMS_OUTPUT.PUT_LINE('tara cu papulatia >0 este:' || v_nume); END IF; END;
tara cu papulatia >0 este:United Kingdom of Great Britain and Northern Ireland Statement processed.

C. DECLARE v_populatie wf_countries.population%TYPE; v_nume wf_countries.country_name%TYPE; BEGIN SELECT population,country_name INTO v_populatie,v_nume FROM wf_countries WHERE country_id=672; IF v_populatie=0

Session 4

THEN DBMS_OUTPUT.PUT_LINE('tara cu papulatia = 0 este:' || v_nume); END IF; END;


tara cu papulatia = 0 este:Antarctica Statement processed

D. DECLARE v_populatie wf_countries.population%TYPE; BEGIN SELECT population INTO v_populatie FROM wf_countries WHERE country_id=15; IF v_populatie IS NULL THEN DBMS_OUTPUT.PUT_LINE('fara date'); END IF; END;
fara date Statement processed.

7. Examine the following code: DECLARE v_country_id wf_countries.country_name%TYPE := <a value>; v_ind_date wf_countries.date_of_independence%TYPE; v_natl_holiday wf_countries.national_holiday_date%TYPE; BEGIN SELECT date_of_independence, national_holiday_date

Session 4

INTO v_ind_date, v_natl_holiday FROM wf_countries WHERE country_id=v_country_id; IF v_ind_date IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('A'); ELSIF v_natl_holiday IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('B'); ELSIF v_natl_holiday IS NULL AND v_ind_date IS NULL THEN DBMS_OUTPUT.PUT_LINE('C'); END IF; END;

A. What would print if the country has an independence date equaling NULL and a national holiday date equaling NULL? --- C B. What would print if the country has an independence date equaling NULL and a national holiday date containing a value? ------- B C. What would print if the country has an independence date equaling a value and a national holiday date equaling NULL? ---- A D. Run a SQL script against the WF_COUNTRIES table to determine whether the following countries have independence dates or national holiday dates, or both. Country Country_ID Independenc National Output e Date? Y/N Holiday should be Date? Y/N 1 United States Y Y Iraq 964 Y N Antarctica 672 N N Spain 34 N Y

Session 4

8. Examine the following code. What output do you think it will produce? DECLARE v_num1 NUMBER(3) := 123; v_num2 NUMBER; BEGIN IF v_num1 <> v_num2 THEN DBMS_OUTPUT.PUT_LINE('The two numbers are not equal'); ELSE DBMS_OUTPUT.PUT_LINE('The two numbers are equal'); END IF; END; Enter and run the script to check if your answer was correct.
The two numbers are equal Statement processed.

Extension Activity 1. Write a PL/SQL block to accept a year and check whether it is a leap year. For example, if the year entered is 1990, the output should be 1990 is not a leap year. Hint: The year should be exactly divisible by 4 but not divisible by 100, or it should be divisible by 400. Test your solution with the following years: 1990 2000 1996 1900 1992 1884

Not a leap year Leap year Leap year Not a leap year Leap year Leap year

Session 4

Lectia 2
1. Write a PL/SQL block: A. To find the number of airports from the wf_countries table for a supplied country_name. Based on this number, display a customized message as follows: # Airports 0100 1011,000 10011,0000 > 10,000 No value in database Message There are 100 or fewer airports. There are between 101 and 1,000 airports. There are between 1,001 and 10,000 airports. There are more than 10,000 airports. The number of airports is not available for this country.

DECLARE v_country_name wf_countries.country_name%TYPE:='Canada'; v_airports wf_countries.airports%TYPE; BEGIN SELECT airports INTO v_airports FROM wf_countries WHERE country_name=v_country_name; CASE WHEN v_airports BETWEEN 100 AND 1000 THEN DBMS_OUTPUT.PUT_LINE('Tara cu mai putine de 100 aeropoarte este' ||v_country_name); WHEN v_airports BETWEEN 101 AND 1000 THEN

Session 4 DBMS_OUTPUT.PUT_LINE('tara cu aeropaorte intre 101 si 1000 este' ||v_country_name); WHEN v_airports BETWEEN 1001 AND 10000 THEN DBMS_OUTPUT.PUT_LINE('tara care are intre 1001 si 10.000 aeropoarte este ' ||v_country_name); WHEN v_airports >10000 THEN DBMS_OUTPUT.PUT_LINE('tara cu peste 10.000 este ' || v_country_name); ELSE DBMS_OUTPUT.PUT_LINE('nu se regaseste in baza de date'); END CASE; END;
tara care are intre 1001 si 10.000 aeropoarte este Canada Statement processed.

2. Write a PL/SQL block: A. To find the amount of coastline for a supplied country name. Use the wf_countries table. Based on the amount of coastline for the country, display a customized message as follows: Length of Coastline Message 0 no coastline <1000 a small coastline <10000 a mid-range coastline All other values a large coastline

Session 4 DECLARE v_country_name wf_countries.country_name%TYPE :='Canada'; v_coastline wf_countries.coastline %TYPE; v_coastline_description VARCHAR2(50); BEGIN SELECT coastline INTO v_coastline FROM wf_countries WHERE country_name = v_country_name; v_coastline_description := CASE WHEN v_coastline=0 THEN 'no coastline' WHEN v_coastline<1000 THEN 'a small coastline' WHEN v_coastline<1000 THEN 'a mid-range coastline' ELSE 'a large coastline' END; DBMS_OUTPUT.PUT_LINE('TARA ' || v_country_name || ' ARE ' || v_coastline_description); END;
TARA Canada ARE a large coastline Statement processed.

3. Use a CASE statement: A. Write a PL/SQL block to select the number of countries using a supplied currency name. If the number of countries is greater than 20, display More than 20 ccountries. If the number of countries is between 10 and 20, display Between 10 and 20 countries. If the number of countries is less than 10, display Fewer than 10 countries. Use a CASE statement.
DECLARE v_currency wf_countries.currency_code%TYPE:='EUR'; v_numar NUMBER; v_tara VARCHAR2; BEGIN SELECT COUNT(currency_code) INTO v_numar, v_tara FROM wf_countries; CASE v_tara WHEN v_numar>20 THEN DBMS_OUTPUT.PUT_LINE('more that 20 countries'); WHEN v_numar BETWEEN 20 AND 10 THEN DBMS_OUTPUT.PUT_LINE('between10 and 20 countries');

Session 4 WHEN v_numar<10 THEN DBMS_OUTPUT.PUT_LINE('fewer than 10 countries'); END CASE; END;

4. Examine the following code. A. What do you think the output will be? Test your theory by running the code in Application Express. DECLARE x BOOLEAN := FALSE; y BOOLEAN; v_color VARCHAR(20) := 'Red'; BEGIN IF (x OR y) THEN v_color := 'White'; ELSE v_color := 'Black'; END IF; DBMS_OUTPUT.PUT_LINE(v_color); END;
Black Statement processed

B. Change the declarations to x and y as follows. What do you think the output will be? Test your theory by running the code again. x BOOLEAN ; y BOOLEAN ; DECLARE x BOOLEAN ; y BOOLEAN; v_color VARCHAR(20) := 'Red'; BEGIN IF (x OR y) THEN v_color := 'White'; ELSE v_color := 'Black'; END IF; DBMS_OUTPUT.PUT_LINE(v_color); END;
Black Statement processed.

Session 4

C. Change the declarations to x and y as follows. What do you think the output will be? Test your theory by running the code again. x BOOLEAN := TRUE; y BOOLEAN := TRUE; DECLARE x BOOLEAN := TRUE; y BOOLEAN := TRUE; v_color VARCHAR(20) := 'Red'; BEGIN IF (x OR y) THEN v_color := 'White'; ELSE v_color := 'Black'; END IF; DBMS_OUTPUT.PUT_LINE(v_color); END;
White Statement processed.

D. Experiment with changing the OR condition to AND. DECLARE x BOOLEAN := TRUE; y BOOLEAN := TRUE; v_color VARCHAR(20) := 'Red'; BEGIN IF (x AND y) THEN v_color := 'White'; ELSE v_color := 'Black'; END IF; DBMS_OUTPUT.PUT_LINE(v_color); END;
White Statement processed.

Session 4

Lectia 3

4. Write a PL/SQL block to display the country_id and country_name values from the WF_COUNTRIES table for country_id whose values range from 1 through 3. Use a basic loop. Increment a variable from 1 through 3. Use an IF statement to test your variable and EXIT the loop after you have displayed the first 3 countries
DECLARE v_nume wf_countries.country_name%TYPE:='Canada'; v_id wf_countries.country_id%TYPE:=1; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('Tara: ' || v_nume ||' cu id: ' || v_id ); v_id:=v_id+1; IF (v_id>3) THEN EXIT; END IF; END LOOP; END;
Tara: Canada cu id: 1 Tara: Canada cu id: 2 Tara: Canada cu id: 3

Session 4

5. Modify your solution to question 4 above, replacing the IF statement with an EXIT....WHEN statement.
DECLARE v_nume wf_countries.country_name%TYPE:=Canada; v_id wf_countries.country_id%TYPE:=1; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('Tara: ' || v_nume ||'cu id: ' || v_id ); v_id:=v_id+1; EXIT WHEN v_id>3; END LOOP; END;
Tara: Canada cu id: 1 Tara: Canada cu id: 2 Tara: Canada cu id: 3

6. Create a Messages Table and insert several rows into it: A. To create the messages table. DROP TABLE messages; CREATE TABLE messages (results NUMBER(2)); B.Write a PL/SQL block to insert numbers into the messages table. Insert the numbers 1 through 10, excluding 6 and 8.
DECLARE v_numar NUMBER(2):=1; BEGIN LOOP INSERT INTO messages(results) VALUES(v_numar+1); v_numar:=v_numar+1; EXIT WHEN v_numar>10; END LOOP;

Session 4 END;

C. Execute a SELECT statement to verify that your PL/SQL block worked.

Session 4

Lectia 4

1. Write a PL/SQL block to display the country_id and country_name values from the WF_COUNTRIES table for country_id whose values range from 51 through 55. Use a WHILE loop. Increment a variable from 51 through 55. Test your variable to see when it reaches 55. EXIT the loop after you have displayed the 5 countries

DECLARE v_id wf_countries.country_id%TYPE:=51; v_nume wf_countries.country_name%TYPE; BEGIN SELECT country_id,country_name INTO v_id,v_nume FROM wf_countries WHERE country_id=v_id; WHILE v_id <=55 LOOP DBMS_OUTPUT.PUT_LINE(v_nume || v_id); v_id:=v_id+1; END LOOP; END;
Republic Republic Republic Republic Republic of of of of of Peru51 Peru52 Peru53 Peru54 Peru55

Statement processed.

Session 4

2. Write a PL/SQL block to display the country_id and country_name values from the WF_COUNTRIES table for country_id whose values range from 51 through 55 in the reverse order. Use a FOR loop.
DECLARE v_id wf_countries.country_id%TYPE; v_nume wf_countries.country_name%TYPE; BEGIN SELECT country_id,country_name INTO v_id,v_nume FROM wf_countries WHERE country_id=v_id; FOR i IN REVERSE 51..55 LOOP DBMS_OUTPUT.PUT_LINE(v_nume||v_id); END LOOP; END;

3. A. Execute the following statements to build a new_emps table. DROP TABLE new_emps; CREATE TABLE new_emps AS SELECT * FROM employees; ALTER TABLE new_emps ADD stars VARCHAR2(50);

B. Create a PL/SQL block that inserts an asterisk in the stars column for every whole $1000 of an employees salary. For example, if an employee has salary of $7800, the string ******* would be inserted. Use the following code as a starting point.

Session 4

DECLARE v_empno new_emps.employee_id%TYPE; v_asterisk new_emps.stars%TYPE := NULL; v_sal_in_thousands new_emps.salary%TYPE; BEGIN SELECT NVL(TRUNC(salary/1000), 0) INTO v_sal_in_thousands FROM new_emps WHERE employee_id = v_empno; FOR v_empno IN 124..142 LOOP UPDATE new_emps SET stars = v_asterisk WHERE employee_id = v_empno; END LOOP; END; END;

A. Test your code using employee_ids 124 and 142. B. Execute a SQL statement to check your results.

Lectia 5

1. Write a PL/SQL block to produce a list of available vehicle license plate numbers. These numbers must be in the following format: NN-MMM, where NN is between 60 and 65, and MMM is between 100 and 110. Use nested FOR loops. The outer loop should choose numbers between 60 and 65. The inner loop should choose numbers between 100 and 110, and concatenate the two numbers together. BEGIN FOR v_licenta IN 60..65 LOOP FOR v_licentav IN 100..110 LOOP DBMS_OUTPUT.PUT_LINE(v_licenta ||'-'|| v_licentav); END LOOP; END LOOP; END;
60-100 60-101 60-102 60-103 60-104 60-105 60-106 60-107

Session 4
60-108 60-109 60-110 61-100 61-101 61-102 61-103 61-104 61-105 61-106 61-107 61-108 61-109 61-110 62-100 62-101 62-102 62-103 62-104 62-105 62-106 62-107 62-108 62-109 62-110 63-100 63-101 63-102 63-103 63-104 63-105 63-106 63-107 63-108 63-109 63-110 64-100 64-101 64-102 64-103 64-104 64-105 64-106 64-107 64-108 64-109 64-110 65-100 65-101 65-102 65-103 65-104 65-105 65-106 65-107 65-108 65-109

Session 4
65-110 Statement processed.

2. Modify your block from question 1 to calculate the sum of the two numbers on each iteration of the inner loop (for example, 62-107 sums to 169), and exit from the OUTER loop if the sum of the two numbers is greater than 172. Use loop labels. Test your modified code.
DECLARED v_sum NUMBER(5); BEGIN FOR v_licenta IN 60..65 LOOP FOR v_licentav IN 100..110 LOOP v_sum:=v_licenta+v_licentav; DBMS_OUTPUT.PUT_LINE(v_licenta ||'-'|| v_licentav|| '=' ||v_sum); END LOOP; EXIT WHEN v_sum>172; END LOOP; END;

Você também pode gostar