SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How to Add Second Function To Trigger?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Palermo
Starting Member

20 Posts

Posted - 11/16/2013 :  15:23:58  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
5937 Posts

Posted - 11/16/2013 :  23:14:45  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 11/17/2013 :  11:40:03  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
5937 Posts

Posted - 11/18/2013 :  10:15:31  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 11/19/2013 :  01:34:20  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000