I have a trigger if I do multiple insert then I get an error on:
IF((SELECT FACTREGELPRIJS FROM inserted) > 0)
This is my trigger:
CREATE TRIGGER [dbo].[trg]
ON [dbo].[FACTUURREGEL]
AFTER INSERT
AS
BEGIN
DECLARE @rowCount INT = @@ROWCOUNT
IF(@rowCount = 0)
RETURN
BEGIN TRY
IF(EXISTS(SELECT 1 FROM inserted)
AND NOT EXISTS(SELECT 1 FROM deleted))
BEGIN
IF((SELECT FACTREGELPRIJS FROM inserted) > 0)
BEGIN
RAISERROR('MAG geen prijs toegevoegd worden',16,1)
END
ELSE
BEGIN
UPDATE f
SET FACTREGELPRIJS = (p.PRODUCTPRIJS * f.FACTREGELHOEVEELHEID) - ((p.PRODUCTPRIJS * f.FACTREGELHOEVEELHEID)*(f.FACTREGELKORTING/100.0))
FROM FACTUURREGEL f
INNER JOIN PRODUCT p on p.PRODUCTID = f.PRODUCTID
END
END
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
DECLARE
@ErrorMessage VARCHAR(400),
@ErrorSeverity INT,
@ErrorState INT
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE()
RAISERROR (@ErrorMessage , @ErrorSeverity, @ErrorState)
END CATCH
END
How can I fix it? thank you
max(...)) forifcondition and you have to join toinsertedpseudo-table to perform updates. Remember that triggers are fired for entire set of changes, not for every record.