I am trying to do an update in SQL Server. I have a temporary table with 50 users. The table contains a personalid and a date. I want to update the main tables date, with the date from the temporary table based on the personalid of the temp table. The main table has 3000+ users.
The logic is to update the users date (which is different for each user) in the main table using the date from the temp table when the personalid exists in the temp table and the main table.
The query always returns a subquery error for too many records and it tries to update all of the records in the main table.
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I have tried using cursors, where and for loops and FETCH etc:
BEGIN TRAN
UPDATE MainTable
SET MainTable_Date = (SELECT t.Date FROM TempTable t
INNER JOIN MainTable m
ON t.PersonalId = m.personalid
WHERE t.PersonalId = m.personalid)
FROM
MainTable AS m
INNER JOIN
TempTable t ON m.personal_id = t.PersonalId
WHERE
m.personal_id IN (SELECT personalId FROM TempTable)
ROLLBACK TRAN
mtwice forMainTable? Andt? Your subquery is thus made independent of the outer query.