-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivraria.sql
More file actions
145 lines (117 loc) · 3 KB
/
Copy pathlivraria.sql
File metadata and controls
145 lines (117 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
DELIMITER $$
create procedure preco_livros()
begin
select titulo,preco from livros;
end
$$
create procedure seleciona_livros (in quantidade int)
begin
select * from livros
limit quantidade;
end $$
create procedure verificar_qtd_livros(out quantidade int)
begin
select count(*)into quantidade
from livros;
end $$
create procedure precomedio(in numero_livro int, out preco_medio float)
begin
declare valor_total float;
declare preco_livro float;
declare finished int default 0;
declare livro_cursor
cursor for select preco from livros;
declare continue handler
for not found set finished =1;
open livro_cursor;
set valor_total=0;
set preco_medio=0;
get_preco:loop
fetch livro_cursor into preco_livro;
if finished =1 then
leave get_preco;
end if;
set valor_total=valor_total+preco_livro;
end loop;
close livro_cursor;
set preco_medio=valor_total/numero_livro;
end $$
create function hello(s char(20))
returns char(50) deterministic
return concat('Hello',s,'!1!');
$$
create function qtd_livros()
returns float deterministic
begin
declare quantidade int;
select count(*) into quantidade from livros;
return quantidade;
end$$
create procedure qtd_livros(out qtd float)
begin
declare totl float;
declare livro_p float;
declare finished int default 0;
declare livro_cursor cursor for select preco from livros;
declare continue handler
for not found set finished =1;
open livro_cursor;
set totl =0;
get_tot:loop
fetch livro_cursor into livro_p;
if finished =1 then
leave get_tot;
end if;
set totl=totl+livro_p;
end loop;
set qtd=totl;
close livro_cursor;
end$$
create function ver_livros_cpf(cpfi int)
returns int deterministic
begin
declare qtd_livros int;
select (select count(codLivro)
from autores_livros where matriculaAutor=matricula) into qtd_livros
from autores where cpf=cpfi;
return qtd_livros;
end$$
create procedure insere_assunto(in sigla char(1),in descricao varchar(50))
begin
insert into assuntos
values(sigla,descricao);
end$$
create procedure muda_data(in cod int, in data_lance date)
begin
update livros
set lancamento=data_lance
where codLivro=cod;
end$$
create procedure atualiza_assunto(in sigla char(1), in dscricao varchar(50))
begin
update assuntos
set descricao =dscricao
where sigla=sigla;
end$$
delimiter ;
call exclui_assunto('x');
select *from v_contas_livros;
select * from autores;
select ver_livros_cpf(22222222222);
drop function ver_livros_cpf;
drop function ver_livros;
select * from livros;
call muda_data(2,'2010-10-01');
drop procedure qtd_livros;
drop procedure precomedio;
call insere_assunto('x','Revista');
call qtd_livros(@totl);
select @totl;
call preco_livros();
call seleciona_livros(2);
call verificar_qtd_livros(@total);
select @total;
call precomedio(@total,@preco);
select @preco;
select hello('rubens');
select qtd_livros();