SQL Server Performance Forum – Threads Archive
trigger with if statement
Hello, i have the following trigger. I want to add functionality so that the only time it updates the fiscal year on the case table is when case.fiscal year is null. I don’t want it to overwrite a value already in case.fiscalyear. I’m having trouble figuring out how to do this. Any help is appreciated. CREATE TRIGGER [DupData] ON [dbo].[Proj]AFTER UPDATE
AS
IF (UPDATE(FiscalYear))
BEGIN
IF EXISTS(select * from [Case] INNER JOIN inserted on [Case].CaseID = inserted.ProjID)
BEGIN
update [Case] set [Case].[FiscalYear] = inserted.FiscalYear
from [Case] INNER JOIN inserted on [Case].CaseID = inserted.ProjID
END Thanks in advance, ben
Easy, just add
WHERE [Case].FiscalYear IS NULL edit: … both to the EXISTS check and the actual UPDATE statement.
Thanks, that’s what I thought it was, but I had only added it to the exists part, not the update part. It’s working now that I’ve added it to both. Thanks again, Ben
]]>