SQLTeam.com | Weblogs | Forums

Limitar insert mensal no sqlserver 2008

Ola bom dia .
Queria ente der se tem como limitar a entrada de informação de uma tabela por coluna .
Ex.
É um estacionamento que tem 30 vagas .
A marcação é online para agendamento de vaga.
Cada cliente que marcar eu guardo o dia mês e ano .

Quando chegar 30 agendamentos no dia X o sql não inserir mais reservas .

You can put a trigger on the table to prevent it. Here's an example

Create table T (CustNo int, DateReserved date, active bit)

insert into T values
(1, '11/4/2021',1),
(2, '11/4/2021',1),
(3, '11/4/2021',1),
(4, '11/4/2021',1),
(5, '11/4/2021',1),
(6, '11/4/2021',1),
(7, '11/4/2021',1),
(8, '11/4/2021',1),
(9, '11/4/2021',1),
(10, '11/4/2021',1),
(11, '11/4/2021',1),
(12, '11/4/2021',1),
(13, '11/4/2021',1),
(14, '11/4/2021',1),
(15, '11/4/2021',1),
(16, '11/4/2021',1),
(17, '11/4/2021',1),
(18, '11/4/2021',1),
(19, '11/4/2021',1),
(20, '11/4/2021',1)

go

Alter TRIGGER [AfterInsertUpdateT_trg]
ON T
AFTER Insert, Update
AS
BEGIN
declare @c int

select @c = count(1)
from t t
join inserted i
on t.DateReserved = i.DateReserved
and t.active = 1
and i.active = 1

if @c > 20
begin
rollback
print ('cannot have more than 20 reservations for the day')
end
END
GO

ALTER TABLE t ENABLE TRIGGER [AfterInsertUpdateT_trg]
GO