I’m trying to create a T-SQL query for several hours now. Google and the forum search did not help, so I hope someone can help me with this.
There are two tables, Change and Journal. Each row in Change has 5 related rows in Journal.
What I want to do is to copy the column value (CreatedDateTime) of a specific related row from Journal into the column AuthorizationDate of Change. This should be done for all rows in Change.
This is what I’ve achieved so far:
UPDATE Change
SET Change.ap_ITSM_MDP_AuthorizationDate =
( SELECT Journal.CreatedDateTime
FROM Journal
JOIN Change
ON Journal.Parentlink_RecID = Change.RecID
WHERE ITSM_ChangeNotes_Subject = 'Status changed to: Authorized'
AND Change.RecID = Journal.ParentLink_RecID
)
Unfortunately, I get the following error message:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
I thought I DID limit the result to one by the WHERE clause (ChangeNotes_Subject = '...'). What did I do wrong?
I think this will work:
But using
JOINis better – to avoid any unwanted updates. Here’s how: