Você está na página 1de 3

----------------------------------------------------------

Create or alter proc up_insert_semestre


@codigo char(6), @f_inicio date, @f_fin date, @retorno int output
as
Begin try
insert into semestre values(@codigo,@f_inicio,@f_fin,0)
set @retorno=0
End try
Begin catch
set @retorno=-1
End catch
----------------------------------------------------------
--delete from semestre where codigo_semestre='2024-0'
Declare @respuesta int
exec up_insert_semestre '2024-1','9-01-2024','28-02-2024', @respuesta output
Select @respuesta

select * from semestre order by 1 desc

----------------------------------------------------------
Create or alter proc up_update_semestre
@codigo char(6), @f_inicio date, @f_fin date, @retorno int output
as
Begin try
update semestre set f_inicio=@f_inicio, f_fin=@f_fin where
codigo_semestre=@codigo
set @retorno=0
End try
Begin catch
set @retorno=-1
End catch
----------------------------------------------------------
Create or alter proc up_delete_semestre
@codigo char(6), @retorno int output
as
Begin try
delete from semestre where codigo_semestre=@codigo
set @retorno=0
End try
Begin catch
set @retorno=-1
End catch
----------------------------------------------------------
create or alter procedure up_activar_semestre
@semestre char(6), @retorno int output
as
Begin
--select count(*) from semestre where codigo_semestre='2024-1'
if exists(select * from semestre where codigo_semestre=@semestre)
begin
update semestre set estado=1 where codigo_semestre=@semestre
update semestre set estado=0 where codigo_semestre<>@semestre
set @retorno=0
end
else
set @retorno=-1
end
----------------------------------------------------------
TRIGGER --> DESENCADENADORES --> DISPARADORES
----------------------------------------------------------

Select * from grupo

alter table grupo add matriculados int

select count(*) from Detalle_matricula where grupoID=2

update grupo
set matriculados=(select count(*) from Detalle_matricula where
grupoID=grupo.grupo_id)

select gr.*, cu.nombre_curso


from grupo gr inner join curso cu on gr.curso_id=cu.curso_id
where semestre_id='2023-2' and cu.ciclo_academico=6
---------------------------------------------------------------------
Create or alter proc up_insert_detalle_matricula
@mat int, @gru int, @retorno int output
as
Begin try
insert into Detalle_matricula values(@mat, @gru, getdate(), 'N', Null)
--update grupo set matriculados=matriculados+1 where grupo_id=@gru
set @retorno=0
End try
Begin catch
set @retorno=-1
End catch
---------------------------------------------------------------------
select * from Detalle_matricula

select * from Matricula


--matricula_id=2,3,5,6,8,10,11,12

Declare @ret int


execute up_insert_detalle_matricula 3,645,@ret
print @ret

select * from grupo where grupo_id=645

insert into Detalle_matricula values(12, 645, getdate(), 'N', Null)

Select * from grupo where grupo_id=645

Create or alter trigger tr_insert_det_matricula


ON Detalle_matricula
after insert
AS
Begin
Declare @gru int
set @gru=(select grupoid from inserted)
update grupo set matriculados=matriculados+1 where grupo_id=@gru
end

--drop trigger tr_insert_det_matricula


82,84,85

insert into Detalle_matricula values(82, 645, getdate(), 'N', Null)

Declare @ret int


execute up_insert_detalle_matricula 85,645,@ret
print @ret

Create table Avance_academico


(
avance_id int primary key identity,
alumno_id int not null references alumno,
curso_id int not null references curso,
semestre_id char(6) references semestre,
nota int
)
select * from Avance_academico
--update curso set plan_estudio=2023
Select top 1 * from alumno
alumno_id <-- 140

insert into Avance_academico(alumno_id,curso_id)


select 39,curso_id from curso where plan_estudio=2023 and escuela_id=3

Create or alter trigger tr_insert_alumno


ON alumno
after insert
AS
Begin
Declare @alu int, @esc int, @pla int
select @pla=plan_estudio,@esc=escuelaid,@alu=alumnoid from inserted
-----
insert into Avance_academico(alumno_id,curso_id)
select @alu,curso_id from curso where plan_estudio=@pla and escuela_id=@esc
-----
end

select * from avance_academico

select top 1 * from Detalle_matricula

Você também pode gostar