0% found this document useful (0 votes)
80 views6 pages

Ubd03 - SQL in PLSQL

Download as pdf or txt
0% found this document useful (0 votes)
80 views6 pages

Ubd03 - SQL in PLSQL

Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

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?

It fails because returns more than requested number of rows.

B. Now modify the block to use ‘IT_PRAG’ instead of ‘IT_PROG’ and re-run it. Why does
it still fail?

It fails because no data found.

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;

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_dept_name new_depts.department_name%TYPE:= 'A New
Department' ; v_dept_id
new_depts.department_id%TYPE; 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;

4. Modify the code to add 10 to the current maximum department number and assign the result to
v_dept_id.
DECLARE
v_dept_name new_depts.department_name%TYPE:= 'A New Department'
;
v_dept_id new_depts.department_id%TYPE;
v_max_deptno new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) + 10 INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '||
v_max_deptno);
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_dept_name new_depts.department_name%TYPE:= 'A New Department'
;
v_dept_id new_depts.department_id%TYPE;
v_max_deptno new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) + 10 INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '||
v_max_deptno);
INSERT INTO new_depts 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_dept_name new_depts.department_name%TYPE:= 'A New Department'
;
v_dept_id new_depts.department_id%TYPE;
v_max_deptno new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) + 10 INTO v_max_deptno
FROM new_depts;
DBMS_OUTPUT.PUT_LINE('The maximum department id is: '||
v_max_deptno);
INSERT INTO new_depts VALUES (v_dept_id, v_dept_name, NULL,
NULL);
DBMS_OUTPUT.PUT_LINE('The number of rows that are inserted are'
||' ' || SQL%ROWCOUNT);
END;

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_dept_name new_depts.department_name%TYPE:= 'A New Department'
;
v_dept_id new_depts.department_id%TYPE;
v_max_deptno new_depts.department_id%TYPE;
BEGIN
SELECT MAX(department_id) + 10 INTO v_max_deptno
FROM new_depts;
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('The number of rows that are inserted are'
||' ' || SQL%ROWCOUNT);
END;

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);

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;

I think that will saved in database the first value.

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;

The program it will faild.

13. Run the block to test your theory.

You might also like