Documentos de Académico
Documentos de Profesional
Documentos de Cultura
0000669667.25 04 19
0000669667.25 04 19
use jugadores
go
--a )Crea 2 tablas (titulares - suplentes)
-- con un procedimiento, parámetro tabla, debe crear dinámicamente
-- (consultando el catálogo para ver como se compone la clave primaria y además el tipo de
dato).
-- Crear la restricción clave primaria
create procedure pr_crear_tabla
@tabla varchar(128)
as
--variables de soporte
declare @objectId int, @createTableQuery varchar(500), @primaryKeyColumns
varchar(500), @lengthQuery varchar(3)
--obtengo el object id de las tabla jugadores
set @objectId = (select object_id from sys.tables where name = 'jugadores')
open cursor_composicion_clave_primaria
fetch next from cursor_composicion_clave_primaria into @nombre, @tipoDato,
@maxLength
while @@FETCH_STATUS = 0
begin
set @lengthQuery = ''
if (@tipoDato in('varchar', 'char'))
begin
set @lengthQuery = '('+@maxLength+')'
end
set @createTableQuery = @createTableQuery + @nombre + ' ' + @tipoDato
+ @lengthQuery + ' not null,'
set @primaryKeyColumns = @primaryKeyColumns + @nombre + ','
fetch next from cursor_composicion_clave_primaria into @nombre,
@tipoDato, @maxLength
end
close cursor_composicion_clave_primaria
deallocate cursor_composicion_clave_primaria
print @createTableQuery
exec (@createTableQuery)
return 0
go
--b) Inserta 11 jugadores como titulares para cada equipo, restantes como suplentes
-- invocando funcion, con parametros equipo, categoria. Retorna n jugadores mas
viejos (correlacionado)
-- cargar las tablas creadas con una función que busca los jugadores más viejos de
forma correlacionada.
-- Funcion tipo tabla ya que retorna un listado de los n jugadores mas viejos del club
recibido
create function dbo.fn_jugadores (@n int, @equipo varchar(30), @categoria int)
returns table
as
return (select * from jugadores j where j.id_club = (select id_club from clubes where nombre
= @equipo) and j.categoria = @categoria and
@n > (select count(*) from jugadores j2 where j.fecha_nac > j2.fecha_nac
and j2.id_club = (select id_club from clubes where nombre = @equipo) and j2.categoria =
@categoria))
go
--Procedure para iterar todos los equipos y popular la tabla de titulares con la funcion
anterior
create procedure pr_popular_tablas
as
-- variables de soporte
declare @nombreClub varchar(30), @idClub int
declare clubes cursor
for select id_club, nombre from clubes
open clubes
fetch next from clubes into @idClub, @nombreClub
while(@@FETCH_STATUS = 0)
begin
print 'Insertando jugadores titulares en ambas categorias para el club ' +
@nombreClub
insert into titulares (Tipodoc, nrodoc) select Tipodoc, nrodoc from
dbo.fn_jugadores(11, @nombreClub, 84)
insert into titulares (Tipodoc, nrodoc) select Tipodoc, nrodoc from
dbo.fn_jugadores(11, @nombreClub, 85)
fetch next from clubes into @idClub, @nombreClub
end
close clubes
deallocate clubes
open clubes_zona_dos
fetch next from clubes_zona_dos into @idClub
while(@@FETCH_STATUS = 0)
begin
set @nombreClub = (select nombre from clubes where id_club = @idClub)
print 'Eliminando un jugador del club '+ @nombreClub
delete top(1) from jugadores where id_club = @idClub
fetch next from clubes_zona_dos into @idClub
end
close clubes_zona_dos
deallocate clubes_zona_dos
go
if not exists(select * from suplentes s inner join jugadores j on s.nrodoc = j.nrodoc and
s.tipodoc = j.tipodoc where j.id_club = @clubId and j.categoria = @categoria)
begin
print 'No hay suplentes para el club y la categoria seleccionada'
rollback transaction
end
--usamos la funcion del punto 2 para buscar el jugador mas viejo del club
declare @nroDocTitular int, @tipoDocTitular char(3), @nroDocSuplente int,
@tipoDocSuplente char(3)
set @nroDocTitular = (select top(1) nroDoc from dbo.fn_jugadores(1, @nombreClub,
@categoria))
set @tipoDocTitular = (select top(1) tipoDoc from dbo.fn_jugadores(1, @nombreClub,
@categoria))
set @nroDocSuplente = (select top(1) s.nrodoc from suplentes s inner join jugadores j on
s.nrodoc = j.nrodoc and s.tipodoc = j.tipodoc where j.id_club = @clubId and j.categoria =
@categoria)
set @tipoDocSuplente = (select top(1) s.Tipodoc from suplentes s inner join jugadores j on
s.nrodoc = j.nrodoc and s.tipodoc = j.tipodoc where j.id_club = @clubId and j.categoria =
@categoria)
print 'Titular mas viejo que pasa a suplentes ' + @tipoDocTitular + ' ' + convert(varchar(12),
@nroDocTitular) + ' categoria ' + convert(varchar(2),@categoria)
print 'Suplente que pasa a titulares ' + @tipoDocSuplente + ' ' + convert(varchar(12),
@nroDocSuplente) + ' categoria ' + convert(varchar(2),@categoria)
go
--Drops
drop procedure pr_crear_tabla
drop procedure pr_popular_tablas
drop procedure pr_remover_un_titular_por_equipo
drop trigger tr_jugadores_delete
drop function fn_jugadores
drop table suplentes
drop table titulares