I want to create a trigger to prevent values being entered above a certain value.
I have read a little but cannot relate the question below to my own.
Trigger to fire only if a condition is met in SQL Server
Code:
ALTER TRIGGER Tgr_IQRating
ON dbo.Customer
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @IQ int
Select @IQ = IQRATING from dbo.customer
IF (@IQ) > 150
BEGIN
PRINT ('Cannot enter anything higher than 100')
END
ROLLBACK TRANSACTION
END
I've tried it like this
IF (IQRating) > 150
BEGIN
PRINT ('Cannot enter anything higher than 100')
END
ROLLBACK TRANSACTION
But get an error that the column cannot be found. Also, the below fails when I try an update.
IF (SELECT IQRating FROM dbo.customer) > 150
BEGIN
PRINT ('Cannot enter anything higher than 100')
END
Error:
Msg 512, Level 16, State 1, Procedure Tgr_IQRating, Line 16
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Any help would be great.
Thanks,
Jay.