I want to make a trigger for one table that will be used before INSERT
.
I want to check if two columns are NULL and if they are NULL
raise an e
Use instead trigger and inserted table like below and have a try.
CREATE TRIGGER INS_TABLE_1
ON mytable
INSTEAD OF INSERT
AS
BEGIN
DECLARE @fn varchar(50),@ln varchar(50)
SELECT @fn=column1 ,@ln=column12 from inserted
IF (@fn IS NULL OR @ln IS NULL)
BEGIN
RAISERROR ('You are not allowed to Add These Data.', 10, 11)
END
ELSE
INSERT INTO mytable (column1 ,column2) values (@fn,@ln)
END
The inserted table stores copies of the affected rows during INSERT
and UPDATE
statements. Instead of trigger replaces the current INSERT by the trigger definition.