· 6 years ago · May 24, 2019, 05:34 PM
1
2Se șterg produsele din categoria hardware3 care nu au fost comandate. Se afișează numărul de rânduri şterse.
3--Delete the products which have not been ordered at all
4
5SET SERVEROUTPUT ON
6BEGIN
7DELETE * FROM products p
8WHERE not exists (select 1 from orderdetails od where p.productid=od.productid);
9DBMS_OUTPUT.PUT_LINE (SQL%ROWCOUNT || ' deleted rows ');
10ROLLBACK;
11DBMS_OUTPUT.PUT_LINE (SQL%ROWCOUNT || ' affected rows ');
12END;
13/
14După ROLLBACK, atributul SQL%ROWCOUNT devine 0. Rezultatul rulării este:
15
1610 randuri sterse
170 randuri afectate
18
19Se încearcă adăugarea unei regiuni și apoi modificarea denumirii produsului având codul 3. În cazul în care acest produs nu există (comanda update nu realizează nici o modificare) va fi afişat un mesaj corespunzător.
20
21--we try updating a product and if it doesnt exist, a corresponding message will appear
22BEGIN
23INSERT INTO products VALUES(8,'Wheat Flour');
24UPDATE products
25SET productname='Peppers'
26WHERE productid=10;
27IF SQL%NOTFOUND THEN
28DBMS_OUTPUT.PUT_LINE('There is no product with this id');
29END IF;
30ROLLBACK;
31END;
32/
33
34Să se încarce în tabela MESAJE primii 5 angajaţi (id şi nume)
35--Load the table „Messages†with the info of the first 5 clients
36
37CREATE TABLE messages
38(id varchar2(7),
39name varchar2(20)
40city varchar2(20)
41);
42
43DECLARE
44v_id clients.id%type;
45v_name clients.name%type;
46v_city clients.city%type;
47
48CURSOR c IS SELECT clientid, clientname, city FROM clients;
49
50BEGIN
51OPEN c;
52FOR i IN 1..5 LOOP
53FETCH c INTO v_id, v_name, v_city;
54INSERT INTO messages VALUES(v_id, v_name, v_city);
55END LOOP;
56CLOSE c;
57END;
58/
59SELECT * FROM messages;
60
61
62
63
64
65
66
67
68
69
70Exemplu:
71Se afişează printr-un ciclu FOR numele şi salariile angajaţilor din departamentul 60:
72--Display using a cursor the clients based in GALATi
73set serveroutput on
74declare
75cursor c is select clientid, clientname, bankaccount from clients where county='GALATI';
76begin
77dbms_output.put_line('The bank accounts of clients in Galati: ');
78for f in c loop
79dbms_output.put_line('The client with id '||f.clientid||' has the bank account '||f.bankaccount);
80end loop;
81end;
82/
83
84
85
86
87
88
89
90
91
92
93
94
95
96Să se afişeze produsele al căror cantitate totală comandată este mai mare decât o valoare primită drept parametru.
97--Display the products which have sold more units than a parameter value
98SET SERVEROUTPUT ON
99
100DECLARE
101CURSOR c_prod (x NUMBER) IS
102SELECT p.productid, p.productname, sum(od.quantity) total
103FROM products p, orderdetails od
104WHERE p.productid =od.productid
105GROUP BY p.productid, p.productname
106HAVING sum(od.quantity)>x
107ORDER BY total desc;
108
109v_val NUMBER(5);
110f c_prod%rowtype;
111
112BEGIN
113v_val:=500;
114DBMS_OUTPUT.PUT_LINE('Products whose quantity sold is greater than'|| v_val);
115
116IF NOT c_prod%ISOPEN THEN
117OPEN c_prod (v_val);
118END IF;
119
120LOOP
121FETCH c_prod into f;
122EXIT WHEN c_prod%notfound;
123DBMS_OUTPUT.PUT_LINE('Product '||f.productid||', '||f.productname||', has sold ' ||f.total||' units');
124END LOOP;
125CLOSE c_prod;
126END;
127/