Need to pass a value in a SQL trigger from on Update statement to another within the same trigger

Hi,

I have a trigger that updates my detail table. I would like to use the same trigger to also update the parent table based on a value in the detail table. I keep getting an error "The multi-part identifier "Citations.LogDetails.ParentLogNo" could not be bound."

The trigger works when I use a literal numeric value but not when I pass a value.  Is there a way to pass the value from the detail table in the first update statement to the update statement in the second table?

Thanks



/****** Object:  Trigger [Citations].[tgr_LogDetailsLastUpDated]    Script Date: 9/14/2018 7:04:29 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [Citations].[tgr_LogDetailsLastUpDated]
ON [Citations].[LogDetails]

AFTER UPDATE AS
 
  UPDATE Citations.LogDetails
  SET LastUpdatedOn = GETDATE()
  WHERE LogDetailID IN (SELECT DISTINCT LogDetailID FROM Inserted)

 UPDATE Citations.LogHeader
 SET LogHeader.TotPlatesPerLog = (SELECT COUNT(DISTINCT(VehiclePlate)) FROM Citations.LogDetails WHERE ParentLogNo = Citations.LogDetails.ParentLogNo)
 WHERE LogHeader.INT_ID = Citations.LogDetails.ParentLogNo
MichaelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James CrawfordIT AssistantCommented:
Try this for your second update statement (going by what you have for table structures):

update LogHeader
set TotPlatesPerLog = Details.NoPlates
from LogHeader as Header
inner join (select ParentLogNo, count(distinct VehiclePlate) as NoPlates 
			from LogDetails
			group by ParentLogNo) as Details on Header.INT_ID = Details.ParentLogNo

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MichaelAuthor Commented:
Hi James. This did the trick. Thank you so much!
0
Scott PletcherSenior DBACommented:
That SQL is very dangerous.  Keep in mind that when you use an alias for a table name, the original table name no longer applies.  Therefore, you should update the alias name instead.  That is the only perfectly accurate way to do updates with a join:

update Header
set TotPlatesPerLog = Details.NoPlates
from LogHeader as Header
inner join (select ParentLogNo, count(distinct VehiclePlate) as NoPlates
                  from LogDetails
                  group by ParentLogNo) as Details on Header.INT_ID = Details.ParentLogNo


To better see how SQL doesn't even "know" what LogHeader is once an alias is used, try to run this query:

select LogHeader.INT_ID, Details.NoPlates
from LogHeader as Header
inner join (select ParentLogNo, count(distinct VehiclePlate) as NoPlates
                  from LogDetails
                  group by ParentLogNo) as Details on Header.INT_ID = Details.ParentLogNo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.