First of all, your insert
is:
INSERT INTO dbo.Horarios
values(@fecha,@fecha,@fechafin)
It is not good not to indicate the names of the columns in insert
, as it is now, I imagine that the Horarios
table has only 3 columns, but this insert
would not work and there the main problem of not indicate the columns, if we add fields, the insert
written like this, stop working. The optimal syntax would be:
INSERT INTO dbo.Horarios (fecha1, fecha2, fecha3)
values(@fecha,@fecha,@fechafin)
The names are fictitious because I do not really know what they are called in the table.
The other issue is, is the way to calculate a new date based on a start date and duration. You have not clarified in which unit the percentage% of the date is measured, but imagine how likely it is that it be in minutes. If this were the case, you do not need to declare variables to do the calculations and it may not be advisable either. We could directly do the Duracion
of the table insert
that has the row or rows just that they have been inserted. Something like that
ALTER TRIGGER [dbo].[tr_horarios] ON [dbo].[Citas] AFTER INSERT
AS
BEGIN
INSERT INTO dbo.Horarios
SELECT fecha,
fecha,
DATEADD(minute, duracion, fecha)
FROM inserted
END
Finally, you have to be clear, that unlike other database engines, in SQL Server the trigger is executed by sentence, ie if you insert a record or 1,000,000 does not matter, the trigger will execute a once. What you tried to do using variables might eventually work when you insert a single row, but it would not work the same if you inserted it wrong. The form that indicates, that of directly inserting the results of inserted
is consistent with the operation of the triggers.