Your problem is in the bolded section.
UPDATE @EntityDateTimes
SET EventID=(SELECT TOP 1 EventID
FROM tkEntityHistory )
WHERE EntityID=@EntityDateTimes.EntityID
AND [DateTime]=@EntityDateTimes.[DateTime])
Because @EntityDateTimes is not in the FROM clause the Query Processor doesn't know what you mean by @EntityDateTimes.EntityID.
What are you trying to accomplish in this section of code? I thinky you may want the WHERE clause to be part of the subquery, then you would need to move the closing parenthesis to the end of the WHERE clause, and on closer inspection it looks like you have an extra parenthesis.
You might try this
[font="Courier New"]UPDATE @EntityDateTimes
SET EventID=(SELECT TOP 1
EventID
FROM
tkEntityHistory
WHERE
EntityID=@EntityDateTimes.EntityID AND
[DateTime]=@EntityDateTimes.[DateTime]) [/font]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question