'SQL Server trigger for verifying date column doesn't work
I have a table for scheduled surgeries and I am trying to write a trigger that will print a message in case the user inserts a past date in the date column (in the logic that you can not schedule something in the past).
What I wrote won't do anything, so if you can please take a look and give me some advice on how I should write it.
IF OBJECT_ID('TR_verifyProg') IS NOT NULL
DROP TRIGGER TR_verifyProg
GO
CREATE TRIGGER TR_verifyProg
ON Interventii
INSTEAD OF INSERT
AS
BEGIN
IF ((SELECT Interventii.[Data]
FROM Interventii) < GETDATE())
BEGIN
PRINT 'Data interventiei trebuie sa fie minim cea curenta';
END
END
Solution 1:[1]
Thank you for the answers. I changed the if statement by declaring two variables before it and using them in the if clause and now it works.
GO
CREATE TRIGGER TR_verifyProg
ON Interventii
INSTEAD OF INSERT
AS
BEGIN
DECLARE @data date =(SELECT [Data] FROM inserted)
DECLARE @today date =GETDATE()
IF (@data<@today)
BEGIN
PRINT 'Data interventiei trebuie sa fie minim cea curenta!';
END
END
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ioana David |