Answer the question
In order to leave comments, you need to log in
How to create a trigger that would calculate the difference between dates in days and write this number into a field?
Good afternoon to everyone who reads me. Let me tell you right now, I am new to this.
You need to create a trigger that would calculate the difference between dates in days and write this number into a field. Actually wrote a test trigger:
create trigger trigger_set_days on guests
for insert, update
as
declare @Date1 datetime
declare @Date2 datetime
declare @TotalDays int
declare @PassportNumber varchar(50)
begin
set @Date1 = (select CheckInDate from inserted)
set @Date2 = (select CheckOutDate from inserted)
set @PassportNumber = (select PassportNumber from inserted)
if ((GETDATE()) < (@Date1))
begin
set @TotalDays = (select datediff(dd, @Date1, @Date2))
update guests set TotalDays = @TotalDays where [email protected] and [email protected]
end
else
begin
set @TotalDays = (select datediff(dd, GETDATE(), @Date2))
update guests set TotalDays = @TotalDays where [email protected] and [email protected]
end
end
Answer the question
In order to leave comments, you need to log in
The subquery returned more than one value in Force the
subquery to return only one value with WHERE and LIMIT 1.
Also correct the code in places:
set @Date2 = (select CheckOutDate from inserted)
set @PassportNumber = (select PassportNumber from inserted)
set @TotalDays = (select datediff(dd, GETDATE(), @Date2))
set @TotalDays = (select datediff(dd, @Date1, @Date2))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question