Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How to Add Second Function To Trigger?

Author  Topic 

Palermo
Starting Member

25 Posts

Posted - 2013-11-16 : 15:23:58
I have created a trigger to check is value in a field is above 100 :

USE Assessment
GO
CREATE TRIGGER AssessSchema.tr_scorers
ON AssessSchema.scorers
AFTER INSERT
AS
DECLARE @score int
SET @score = (SELECT score FROM INSERTED)
IF @score > 100
BEGIN
DELETE FROM scorers
WHERE scorer = @scorer
PRINT 'insert error score > 100'
END
ELSE
BEGIN
PRINT 'insert success'
END
GO

What's the syntax to add this second check in the same trigger?

IF @score < 0
BEGIN
DELETE FROM scorers
WHERE scorer = @scorer
PRINT 'insert error score < 0'

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-16 : 23:14:45
A trigger can have multiple rows inserted at once so you shouldn't do this:
SET @score = (SELECT score FROM INSERTED)

The way it is coded it will error if there are multiple rows in INSERTED.
And if you do it this way:
select @score = score from inserted
that won't error but you just get the last value that sql looks at among the inserted rows. so that would even be worse.

If you objective is to only allow scores between 0 and 100 it is probably better to use a check constraint on the table instead of a trigger.


Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-17 : 11:40:03
quote:
Originally posted by Palermo

I have created a trigger to check is value in a field is above 100 :

USE Assessment
GO
CREATE TRIGGER AssessSchema.tr_scorers
ON AssessSchema.scorers
AFTER INSERT
AS
DECLARE @score int
SET @score = (SELECT score FROM INSERTED)
IF @score > 100
BEGIN
DELETE FROM scorers
WHERE scorer = @scorer
PRINT 'insert error score > 100'
END
ELSE
BEGIN
PRINT 'insert success'
END
GO

What's the syntax to add this second check in the same trigger?

IF @score < 0
BEGIN
DELETE FROM scorers
WHERE scorer = @scorer
PRINT 'insert error score < 0'


why do you need a trigger for this? Why not just create a CHECK constraint on the column score like below


ALTER TABLE AssessSchema.scorers ADD CONSTRAINT Chk_Score CHECK (score <100)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-18 : 10:15:31
quote:


ALTER TABLE AssessSchema.scorers ADD CONSTRAINT Chk_Score CHECK (score <100)




Thank you for supporting my suggestion
But I think they want this:

ALTER TABLE AssessSchema.scorers ADD CONSTRAINT Chk_Score CHECK (score between 0 and 100)


Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-19 : 01:34:20
quote:
Originally posted by TG

quote:


ALTER TABLE AssessSchema.scorers ADD CONSTRAINT Chk_Score CHECK (score <100)




Thank you for supporting my suggestion
But I think they want this:

ALTER TABLE AssessSchema.scorers ADD CONSTRAINT Chk_Score CHECK (score between 0 and 100)


Be One with the Optimizer
TG


Ah sorry missed that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -