Patrick Rainer

Patrick Rainer

  • NA
  • 35
  • 2.6k

How to Insert only if value is true

Dec 12 2020 4:39 AM
Dear all
 
I am trying to insert only if a specific related value is true. I tried the code below, but was not working so far. How can I achieve this in a trigger?
 
  1. CREATE TRIGGER CreateTest  
  2.     ON Participations  
  3.     AFTER INSERT  
  4.     AS  
  5. begin  
  6.     set nocount on;  
  7.   
  8.     DECLARE @testNeeded bit;  
  9.   
  10.     insert into Tests(participationid)  
  11.     values  
  12.         ((SELECT i.ParticipationId  
  13.           from inserted as i  
  14.                    inner join Executions on i.ExecutionId = i.ExecutionId  
  15.                    inner join Courses C on C.CourseId = Executions.CourseId  
  16.           where c.TestNeeded = 1))  
  17.   
  18. end  
  19. go  
br
Patrick 

Answers (7)