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.
    
        
            
                
                    
                        
                            
                                | 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 AssessmentGOCREATE TRIGGER AssessSchema.tr_scorersON AssessSchema.scorersAFTER INSERTASDECLARE @score intSET @score = (SELECT score FROM INSERTED)IF @score > 100BEGINDELETE FROM scorersWHERE scorer = @scorerPRINT 'insert error score > 100'ENDELSEBEGINPRINT 'insert success'ENDGOWhat's the syntax to add this second check in the same trigger?IF @score < 0BEGINDELETE FROM scorersWHERE scorer = @scorerPRINT '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 insertedthat 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 OptimizerTG  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     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 AssessmentGOCREATE TRIGGER AssessSchema.tr_scorersON AssessSchema.scorersAFTER INSERTASDECLARE @score intSET @score = (SELECT score FROM INSERTED)IF @score > 100BEGINDELETE FROM scorersWHERE scorer = @scorerPRINT 'insert error score > 100'ENDELSEBEGINPRINT 'insert success'ENDGOWhat's the syntax to add this second check in the same trigger?IF @score < 0BEGINDELETE FROM scorersWHERE scorer = @scorerPRINT 'insert error score < 0'
  why do you need a trigger for this? Why not just create a CHECK constraint on the column score like belowALTER TABLE AssessSchema.scorers ADD CONSTRAINT Chk_Score CHECK (score <100) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     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 OptimizerTG  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     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 OptimizerTG
  Ah sorry missed that------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |