-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathej14_ServiciosParaFestejos.sql
More file actions
102 lines (89 loc) · 2.25 KB
/
ej14_ServiciosParaFestejos.sql
File metadata and controls
102 lines (89 loc) · 2.25 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
-- crear base de datos
create database ej14_ServiciosParaFestejos;
-- usar la base de datos
use ej14_ServiciosParaFestejos;
-- crear tablas
create table servicio
(
nroServicio int not null,
descripcion varchar(20) not null,
precio float not null,
primary key(nroServicio)
);
create table cliente
(
nroCliente int not null,
razonSocial varchar(20) not null,
primary key(nroCliente)
);
create table festejo
(
nroFestejo int not null,
descripcion varchar(30) not null,
fecha date not null,
nroCliente int not null,
primary key(nroFestejo),
foreign key(nroCliente) references cliente(nroCliente)
);
create table contrata
(
nroFestejo int not null,
item int not null,
nroServicio int not null,
horaDesde time not null,
horaHasta time not null,
primary key(nroFestejo, item),
foreign key(nroFestejo) references festejo(nroFestejo),
foreign key(nroServicio) references servicio(nroServicio)
);
-- 1
drop procedure p_Servicios;
create procedure p_Servicios(@fechaDesde time, @fechaHasta time)
as
begin try
begin transaction
insert into #festejoPeriodo
select * from festejo f
where f.fecha between @fechaDesde and @fechaHasta;
insert into #servicioTodos
select distinct s.nroServicio from servicio s
where not exists(
select s.nroServicio, f.nroFestejo
from servicio s, festejoPeriodo f
where not exists(
select f.nroServicio, f.nroFestejo
from festejoPeriodo f
)
);
select s.descripcion as nombreServicio, sum(c.horaHasta - c.horaDesde) as horasContratadas
from servicio s
inner join #servicioTodos st
on s.nroServicio = st.nroServicio
inner join contrata c
on s.nroServicio = c.nroServicio
inner join #festejoPeriodo fp
on fp.nroFestejo = c.nroFestejo
group by s.descripcion
commit
end try
begin catch
rollback
end catch;
execute p_Servicios '1/11/2017','7/11/2017';
-- 2
alter table contrata add column tiempo smallint not null default 0;
-- 3
drop trigger tg_Tiempo;
create trigger tg_Tiempo on contrata
after insert
as
begin try
begin transaction
if inserted.horaDesde < inserted.horaHasta
set inserted.tiempo = dateDiff(minute, inserted.horaDesde, inserted.horaHasta)
commit
end try
begin catch
print 'Las horas de contratación están al revés'
rollback
end catch;