I need very simple task - replace date and time in two columns which is to be inserted into table. This spread out to very difficult thing. I tried lot of things, beginning with How can I edit values of an INSERT in a trigger on SQL Server?. Then I read about OUTPUT and tried this:
CREATE TRIGGER RouteSheetRecordDatesCorrection ON dbo.RouteSheetRecords
AFTER INSERT
AS
BEGIN
DECLARE @T TABLE
(
[Id] int,
[AccomplishmentDate] datetime2,
[RouteSheetRecordOperationId] int,
[WorkerName] varchar(50),
[RouteSheetProductId] varchar(72),
[Notes] varchar(500),
[Location] varchar(100),
[CreationDate] datetime2,
[Creator] varchar(30),
[Components] varchar(max)
)
UPDATE dbo.RouteSheetRecords
SET AccomplishmentDate = GETDATE(), CreationDate = GETDATE()
OUTPUT inserted.Id,
inserted.AccomplishmentDate,
inserted.RouteSheetRecordOperationId,
inserted.WorkerName,
inserted.RouteSheetProductId,
inserted.Notes,
inserted.Location,
inserted.CreationDate,
inserted.Creator,
inserted.Components
INTO @T
WHERE Id = (SELECT Id FROM inserted)
END
GO
But no matter what I try, I always get SqlException with this message when inserting new row:
The target table 'dbo.RouteSheetRecords' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.