You are on page 1of 3

Advanced queries (e.g.

Cursors, Control Structures) (2 hours)


Tutorial (Lab session)
1. 3.
-- Create the ART table DECLARE
CREATE TABLE art v_country_name wf_countries.country_name%TYPE;
(id INTEGER NOT NULL UNIQUE, BEGIN
title VARCHAR2(70), SELECT country_name INTO v_country_name
artist VARCHAR2(50), FROM wf_countries WHERE country_id= 359;
description VARCHAR2(100)); DBMS_OUTPUT.PUT_LINE(' The country name is :
'||v_country_name);
-- Insert the three rows on the slide
END;
INSERT INTO art (id, title, artist, description)
VALUES (10, 'Guernica', 'Pablo Picasso', 'oil painting');
INSERT INTO art (id, title, artist, description) 4.
VALUES (20, 'Skriget', 'Edvard Munch', 'oil painting'); DECLARE
INSERT INTO art (id, title, artist, description) v_emp_hiredate employees.hire_date%TYPE;
VALUES (30, 'Femmes de Tahiti (Sur la plage)', 'Paul v_emp_salary employees.salary%TYPE;
Gauguin', 'oil painting'); BEGIN
SELECT hire_date, salary
-- Now the INSERT statement on the slide INTO v_emp_hiredate, v_emp_salary
INSERT INTO art (id, title, artist, description) FROM employees
VALUES (35, 'Mona Lisa', 'Leonardo da Vinci', 'oil WHERE employee_id = 100;
painting'); DBMS_OUTPUT.PUT_LINE('Hiredate is: ' ||
v_emp_hiredate
2. || ' and Salary is: '
-- Create the ITEMS table || v_emp_salary);
CREATE TABLE items END;
(item_id INTEGER NOT NULL UNIQUE,
title VARCHAR2(70),
artist VARCHAR2(50), 5.
description VARCHAR2(100)); DECLARE
-- Insert the rows v_hire_date employees.hire_date%TYPE;
INSERT INTO items (item_id, title, artist, description) employee_id employees.employee_id%TYPE := 176;
VALUES (1, 'Madonna', 'Edvard Munch', 'oil painting'); BEGIN
SELECT hire_date
INSERT INTO items (item_id, title, artist, description) INTO v_hire_date
VALUES (3, 'Femmes de Tahiti (Sur la plage)', 'Paul FROM employees
Gauguin', 'oil painting'); WHERE employee_id = employee_id;
INSERT INTO items (item_id, title, artist, description) END;
VALUES (4, 'Stained Glass window at the United Nations
Building, New York', 6.
'Marc Chagall', 'Stained Glass Window'); -- Create the EMP_DUP table
CREATE TABLE emp_dup AS SELECT * FROM
INSERT INTO items (item_id, title, artist, description) employees;
VALUES (35, 'Mona Lisa', 'Leonardo da Vinci', 'Small
Oil Painting on Wood'); -- Show the names in the table
-- Now the MERGE statement on the slide SELECT first_name, last_name FROM emp_dup;
MERGE INTO art a -- Now execute the anonymous block on the slide
USING items i DECLARE
  ON (a.id = i.item_id) last_name VARCHAR2(25) := 'King';
WHEN MATCHED BEGIN
THEN UPDATE SET DELETE FROM emp_dup WHERE last_name =
   a.artist    = i.artist, last_name;
   a.description   = i.description END;
WHEN NOT MATCHED
THEN INSERT -- Now see which names are still in the table
VALUES(i.item_id, i.title, i.artist, i.description); SELECT first_name, last_name FROM emp_dup;
7. 11.
-- First create the COPY_EMP table DECLARE
CREATE TABLE copy_emp AS SELECT * FROM v_deptno copy_emp.department_id%TYPE := 50;
employees; BEGIN
DELETE FROM copy_emp
-- Now the block on the slide WHERE department_id = v_deptno;
BEGIN DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT ||
INSERT INTO copy_emp ' rows deleted.');
(employee_id, first_name, last_name, email, END;
hire_date, job_id, salary)
VALUES (99, 'Ruth', 'Cores', 12.
'RCORES', SYSDATE, 'AD_ASST', 4000); DECLARE
END;
v_sal_increase employees.salary%TYPE := 800;
8. BEGIN
DECLARE UPDATE copy_emp
v_sal_increase employees.salary%TYPE := 800;
BEGIN SET salary = salary + v_sal_increase
UPDATE copy_emp WHERE job_id = 'ST_CLERK';
SET salary = salary + v_sal_increase
WHERE job_id = 'ST_CLERK'; DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT ||
END; ' rows updated.');
END;
9.
DECLARE 13.
v_deptno employees.department_id%TYPE := 10; -- Create the RESULTS table
BEGIN CREATE TABLE results (num_rows NUMBER(4));
DELETE FROM copy_emp
WHERE department_id = v_deptno; -- Execute the block on the slide
END; BEGIN
UPDATE copy_emp
10.
BEGIN SET salary = salary + 100
MERGE INTO copy_emp c WHERE job_id = 'ST_CLERK';
USING employees e
ON (e.employee_id = c.employee_id) INSERT INTO results (num_rows)
WHEN MATCHED THEN VALUES (SQL%ROWCOUNT);
UPDATE SET END;
c.first_name = e.first_name,
c.last_name = e.last_name, 14.
c.email = e.email, DECLARE
c.phone_number = e.phone_number, v_myage NUMBER:=31;
c.hire_date = e.hire_date, BEGIN
c.job_id = e.job_id, IF v_myage < 11
c.salary = e.salary, THEN
c.commission_pct = e.commission_pct, DBMS_OUTPUT.PUT_LINE('I am a child.');
c.manager_id = e.manager_id, END IF;
c.department_id = e.department_id END;
WHEN NOT MATCHED THEN
INSERT VALUES(e.employee_id, e.first_name, 15.
e.last_name, DECLARE
e.email, e.phone_number, e.hire_date, e.job_id, v_myage NUMBER:=31;
e.salary, e.commission_pct, e.manager_id, BEGIN
e.department_id); IF v_myage < 11
END; THEN
DBMS_OUTPUT.PUT_LINE(' I am a child ');
ELSE
DBMS_OUTPUT.PUT_LINE(' I am not a child ');
END IF; v_emps NUMBER;
END; v_mngid departments.manager_id%TYPE := 108;
BEGIN
CASE v_mngid
WHEN 108 THEN
16. SELECT department_id, department_name
DECLARE INTO v_deptid, v_deptname FROM departments
v_myage NUMBER:=31; WHERE manager_id=108; SELECT count(*)
BEGIN INTO v_emps FROM employees
IF v_myage < 11 WHERE department_id=v_deptid;
THEN -- WHEN 200 THEN --
DBMS_OUTPUT.PUT_LINE('I am a child'); END CASE;
ELSIF v_myage < 20 DBMS_OUTPUT.PUT_LINE ('You are working in the
THEN '|| v_deptname||
DBMS_OUTPUT.PUT_LINE('I am young'); ' department. There are '||v_emps ||' employees in this
ELSIF v_myage < 30 department');
THEN END;
DBMS_OUTPUT.PUT_LINE('I am in my twenties');
ELSIF v_myage < 40 18.
THEN DECLARE
DBMS_OUTPUT.PUT_LINE('I am in my thirties'); v_out_var VARCHAR2(15);
ELSE v_in_var NUMBER;
DBMS_OUTPUT.PUT_LINE('I am always young '); BEGIN
END IF; v_out_var :=
END; CASE v_in_var
WHEN 1 THEN 'Low value'
WHEN 50 THEN 'Middle value'
17.
WHEN 99 THEN 'High value'
DECLARE
ELSE 'Other value'
v_deptid departments.department_id%TYPE;
END;
v_deptname departments.department_name%TYPE;
END;

You might also like