Вы находитесь на странице: 1из 11

Tema nr.

3
Observație!
Scrieți rezolvarea direct în acest document!

1. Enter and run the following anonymous block, observing that it executes successfully.

DECLARE
v_emp_lname employees.last_name%TYPE;
v_emp_salary employees.salary%TYPE;
BEGIN
SELECT last_name, salary INTO v_emp_lname, v_emp_salary
FROM employees
WHERE job_id = 'AD_PRES';
DBMS_OUTPUT.PUT_LINE(v_emp_lname||' '||v_emp_salary);
END;

A. Now modify the block to use ‘IT_PROG’ instead of ‘AD_PRES’ and re-run it. Why does
it fail this time?
Comanda INTO returneaza mai multe valori, cea ce provoaca eroare, se poate
returna doar valorile pentru un singur ID.
B. Now modify the block to use ‘IT_PRAG’ instead of ‘IT_PROG’ and re-run it. Why does
it still fail?
Nu exista un angajati care ar avea acest ID.

The following questions use a copy of the departments table. Execute the following SQL
statement to create the copy table.
CREATE TABLE new_depts AS SELECT * FROM departments;

2. Examine and run the following PL/SQL code, which obtains and displays the maximum
department_id from new_depts.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
END;

The maximum department id is: 190


3. Modify the code to declare two additional variables, (assigning a new department name to one
of them) by adding the following two lines to your Declaration section:
v_dept_name new_depts.department_name%TYPE:= 'A New Department' ;
v_dept_id new_depts.department_id%TYPE;

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:= 'A New Department' ;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
END;

4. Modify the code to add 10 to the current maximum department number and assign the result to
v_dept_id.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:= 'IT' ;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
v_dept_id := v_max_deptno +10;
DBMS_OUTPUT.PUT_LINE('New the maximum department id is: '|| v_dept_id);
END;

5. Modify the code to include an INSERT statement to insert a new row into the new_depts table,
using v_dept_id and v_dept_name to populate the department_id and department_name columns.
Insert NULL into the location_id and manager_id columns. Save your code.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:= 'IT' ;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
v_dept_id := v_max_deptno +10;
DBMS_OUTPUT.PUT_LINE('New the maximum department id is: '|| v_dept_id);
INSERT INTO new_depts(department_id, department_name,location_id,manager_id)
VALUES(v_dept_id, v_dept_name, NULL, NULL);
END;

6. Execute the block and check that the new row has been inserted.

7. Now modify the code to use SQL%ROWCOUNT to display the number of rows inserted, and
execute the block again.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:= 'IT' ;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
v_dept_id := v_max_deptno +10;
DBMS_OUTPUT.PUT_LINE('New the maximum department id is: '|| v_dept_id);
INSERT INTO new_depts(department_id, department_name,location_id,manager_id)
VALUES(v_dept_id, v_dept_name, NULL, NULL);
DBMS_OUTPUT.PUT_LINE('Number of rows modified: ' || TO_CHAR(SQL
%ROWCOUNT));
END;

The maximum department id is: 240


New the maximum department id is: 250
Number of rows modified: 1

Statement processed.

8. Now modify the block, removing the INSERT statement and adding a statement that will
UPDATE all rows with location_id = 1700 to location_id = 1400. Execute the block again to see
how many rows were updated.

DECLARE
v_max_deptno new_depts.department_id%TYPE;
v_dept_name new_depts.department_name%TYPE:= 'IT' ;
v_dept_id new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
v_dept_id := v_max_deptno +10;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '|| v_max_deptno);
UPDATE new_depts
SET LOCATION_ID=1400
WHERE LOCATION_ID=1700;
DBMS_OUTPUT.PUT_LINE('Number of rows modified: ' || TO_CHAR(SQL
%ROWCOUNT));
END;

The maximum department id is: 240


v_dept_id is: 250
Number of rows modified: 4

Statement processed.

9. Create the endangered_species table by running the following statement in Application


Express:

CREATE TABLE endangered_species


(species_id NUMBER(4) CONSTRAINT es_spec_pk PRIMARY KEY,
common_name VARCHAR2(30) CONSTRAINT es_com_name_nn NOT NULL,
scientific_name VARCHAR2(30) CONSTRAINT es_sci_name_nn NOT NULL);

Table created.

10. Examine the following block. If you were to run this block, what data do you think would be
saved in the database?

BEGIN
INSERT INTO endangered_species
VALUES (100, 'Polar Bear','Ursus maritimus');
SAVEPOINT sp_100;
INSERT INTO endangered_species
VALUES (200, 'Spotted Owl','Strix occidentalis');
SAVEPOINT sp_200;
INSERT INTO endangered_species
VALUES (300, 'Asiatic Black Bear','Ursus thibetanus');
ROLLBACK TO sp_100;
COMMIT;
END;

S-a introdus in tabel Polar Bear Ursus Maritimus cu id 100

11. Run the block to test your theory. Select from the table to confirm the result.
12. Examine the following block. If you were to run this block, what data do you think would be
saved in the database?

BEGIN
INSERT INTO endangered_species
VALUES (400, 'Blue Gound Beetle','Carabus intricatus');
SAVEPOINT sp_400;
INSERT INTO endangered_species
VALUES (500, 'Little Spotted Cat','Leopardus tigrinus');
ROLLBACK;
INSERT INTO endangered_species
VALUES (600, 'Veined Tongue-Fern','Elaphoglossum nervosum');
ROLLBACK TO sp_400;
END;

Nu se insereaza nimic deoarece primul rollback sterge toate comenzile inserate ,


deasemenea sterge si savepointul sp_400, la apelearea rollback to sp_400 nu functioneaza
comanda deoarece ea nu exista

13. Run the block to test your theory.

ORA-01086: savepoint 'SP_400' never established in this session or is invalid

14. 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). India’s population should be greater than 1 billion, while United Kingdom’s
should be less than 1 billion.

DECLARE
v_population countries.population%type;
v_country_name countries.country_name%type;
BEGIN
SELECT population,country_name into v_population,v_country_name
FROM countries
Where country_id = 91;
IF(v_population>1000000000) THEN
DBMS_OUTPUT.PUT_LINE('Populatia in '||v_country_name||' este mai mare decat
1000000000');
ELSE
DBMS_OUTPUT.PUT_LINE('Populatia in '||v_country_name||' este mai mica decat
1000000000');
END IF;
END;

Populatia in Republic of India este mai mare decat 1000000000


DECLARE
v_population countries.population%type;
v_country_name countries.country_name%type;
BEGIN
SELECT population,country_name into v_population,v_country_name
FROM countries
Where country_id = 44;
IF(v_population>1000000000) THEN
DBMS_OUTPUT.PUT_LINE('Populatia in '||v_country_name||' este mai mare decat
1000000000');
ELSE
DBMS_OUTPUT.PUT_LINE('Populatia in '||v_country_name||' este mai mica decat
1000000000');
END IF;
END;

Populatia in United Kingdom of Great Britain and Northern Ireland este mai mica decat
1000000000

15. 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;

Eu cred ca va returna prima varianta – The two numbers are not equal deoarece v_num2 nu are
valoare

Enter and run the script to check if your answer was correct.

The two numbers are equal

16. 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_countries NUMBER;
BEGIN
SELECT COUNT(COUNTRY_ID) INTO v_countries
FROM COUNTRIES
WHERE CURRENCY_CODE = (
SELECT CURRENCY_CODE
FROM CURRENCIES
WHERE CURRENCY_NAME = 'Euro');
CASE
WHEN v_countries<10 THEN DBMS_OUTPUT.PUT_LINE('Fewer than 10 countries');
WHEN v_countries between 10 and 20 THEN DBMS_OUTPUT.PUT_LINE('Between 10 and
20 countries');
ELSE DBMS_OUTPUT.PUT_LINE('More than 20 countries');
END CASE;
END

More than 20 countries

17. 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_country_id COUNTRIES.COUNTRY_ID%TYPE;
v_country_name COUNTRIES.COUNTRY_NAME%TYPE;
v_counter number;
BEGIN
v_counter:=1;
LOOP
SELECT COUNTRY_ID,COUNTRY_NAME into v_country_id ,v_country_name
FROM COUNTRIES
WHERE COUNTRY_ID = v_counter;
DBMS_OUTPUT.PUT_LINE(v_country_id||' '||v_country_name );
v_counter:=v_counter+1;
if v_counter>3 then exit;
END IF;
END LOOP;
END;

1 United States of America


2 Canada
3 Republic of Kazakhstan
18. Modify your solution to question 4 above, replacing the IF statement with an EXIT....WHEN
statement.

DECLARE
v_country_id COUNTRIES.COUNTRY_ID%TYPE;
v_country_name COUNTRIES.COUNTRY_NAME%TYPE;
v_counter number;
BEGIN
v_counter:=1;
LOOP
SELECT COUNTRY_ID,COUNTRY_NAME into v_country_id ,v_country_name
FROM COUNTRIES
WHERE COUNTRY_ID = v_counter;
DBMS_OUTPUT.PUT_LINE(v_country_id||' '||v_country_name );
v_counter:=v_counter+1;
EXIT WHEN v_counter > 3;
END LOOP;
END;

19. 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_country_id COUNTRIES.COUNTRY_ID%TYPE;
v_country_name COUNTRIES.COUNTRY_NAME%TYPE;
v_counter number;
BEGIN
v_counter:=51;
WHILE v_counter<=55 LOOP
SELECT COUNTRY_ID,COUNTRY_NAME into v_country_id ,v_country_name
FROM COUNTRIES
WHERE COUNTRY_ID = v_counter;
DBMS_OUTPUT.PUT_LINE(v_country_id||' '||v_country_name );
v_counter:=v_counter+1;
END LOOP;
END;

51 Republic of Peru
52 United Mexican States
53 Republic of Cuba
54 Argentine Republic
55 Federative Republic of Brazil
20. 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_country_id COUNTRIES.COUNTRY_ID%TYPE;
v_country_name COUNTRIES.COUNTRY_NAME%TYPE;
v_counter number;
BEGIN
FOR v_counter IN REVERSE 51..55 LOOP
SELECT COUNTRY_ID,COUNTRY_NAME into v_country_id ,v_country_name
FROM COUNTRIES
WHERE COUNTRY_ID = v_counter;
DBMS_OUTPUT.PUT_LINE(v_country_id||' '||v_country_name );
END LOOP;
END;

55 Federative Republic of Brazil


54 Argentine Republic
53 Republic of Cuba
52 United Mexican States
51 Republic of Peru

21. 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.

DECLARE
NN NUMBER;
MMM NUMBER;
BEGIN
FOR NN IN 60..65 LOOP
FOR MMM IN 100..110 LOOP
DBMS_OUTPUT.PUT_LINE(NN||'-'||MMM );
END LOOP;
END LOOP;
END;

60-100
60-101
60-102
60-103
60-104
60-105
60-106
60-107
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
65-110

Вам также может понравиться