Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a trigger to copy the records of a field from one table to another. I have got two tables namely School and SignOff where School has a primary key SchoolID and the signOff table has a foreign key for the field SchoolID from school. We have got multiple records in the SignOff table for the same SchoolID . I want to copy the field SchoolSignedOffDate from SignOff table to DateTravelPlanSubmitted in school table.

I want to copy field only if the Submitted field in the table SignOff is not null. Till now I have queried as

SQL
USE [TfLStarsDev]
GO
/****** Object:  Trigger [dbo].[Trigger1]    Script Date: 14/01/2016 09:50:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Trigger1]
   ON  [dbo].[SignOff]
   AFTER UPDATE
AS
BEGIN TRY
    BEGIN TRANSACTION
	
	UPDATE School
  SET School.DateTravelPlanSubmitted = SchoolSignDate.SignoffSchoolDateMax
FROM School  INNER JOIN SignOff  ON  School.SchoolID = SignOff.SchoolID
inner join (select SignOff.SchoolID,SignoffSchoolDateMax=max(SignOff.SignedOffBySchoolDate) 
			from SignOff
			inner join School on School.SchoolID=SignOff.SchoolID
			group by SignOff.SchoolID) 
			SchoolSignDate on SchoolSignDate.SchoolID=School.SchoolID

where SignOff.Submitted IS NOT NULL

	 
    COMMIT TRAN 
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRAN 
END CATCH


I manged to copy without the where condition. Now the issue is only with the where condition. If I give as NOT NULL it is showing error.

Can anyone help to fix this issue.
Posted
Updated 14-Jan-16 0:33am
v2

1 solution

Assuming that rest of your trigger is working fine the problem seems to be in the where condition.
SQL
where SignOff.Submitted!=NOT NULL


The correct way of doing same is

SQL
where SignOff.Submitted is NOT NULL


Change the condition and try again
 
Share this answer
 
Comments
user 3008 14-Jan-16 6:34am    
I even tried that, now it is not showing any errors but the where condition is not having any effect in the output
_Asif_ 14-Jan-16 6:40am    
You now need to check your update query, one way of debugging is to change your update to SELECT and see your return result conforms to what you expect, like for example check below query and analyse what is wrong?
SELECT School.DateTravelPlanSubmitted, SchoolSignDate.SignoffSchoolDateMax
FROM School
INNER JOIN SignOff ON School.SchoolID = SignOff.SchoolID
inner join (select SignOff.SchoolID,SignoffSchoolDateMax=max(SignOff.SignedOffBySchoolDate)
from SignOff
inner join School on School.SchoolID=SignOff.SchoolID
group by SignOff.SchoolID)
SchoolSignDate on SchoolSignDate.SchoolID=School.SchoolID
user 3008 14-Jan-16 6:36am    
the field Submitted is a Boolean field and is there any other way to give the where condition so that it will have some effect
user 3008 14-Jan-16 7:26am    
I tried the where condition as SignOff.SignedOffBySchoolDate!=1 in the select statement and it is returning the correct records and I tried using the same where condition in the update statement but it is not having any effect.
Sahan2u 18-Jan-16 1:14am    
Have you tried to execute the update statement outside of the transaction block.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900