1

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
2
  • So do you have one row per user in both tables? Commented Dec 24, 2015 at 10:35
  • Why use the alias m twice for MainTable? And t? Your subquery is thus made independent of the outer query. Commented Dec 24, 2015 at 14:02

3 Answers 3

2

If this fails:

UPDATE m
SET MainTable_Date = t.[Date]
FROM MainTable AS m
INNER JOIN TempTable AS t ON m.personal_id = t.PersonalId

it means that there is not an one-on-one relationship on the tables.

For checking run:

SELECT
    m.personal_id,
    t.PersonalId
FROM MainTable AS m
LEFT JOIN TempTable AS t ON m.personal_id = t.PersonalId
ORDER BY M.personal_id, t.PersonalId
Sign up to request clarification or add additional context in comments.

1 Comment

I do not think the update will fail even if the personal_id occurs more than once in the TempTable. It will just be unclear which value in the temptable will be used if there is more than one occurrence. At least the first statement in your answer solves a lot of no no's in the original query.
1

This query will satisfy ur need i think

CREATE TABLE MainTable
(
Personal_id int,
[MainTable_Date] date
)



CREATE TABLE #TempTable
(
Personalid int,
[Date] date
)



INSERT INTO MainTable
VALUES(1,'20120202'),(2,'20130202'),(3,'20150202'),(4,'20150206')

INSERT INTO #TempTable
VALUES(1,'20120203'),(2,'20130311'),(3,'20100202')


SELECT * FROM MainTable

SELECT * FROM #TempTable

UPDATE MainTable
SET MainTable_Date=(SELECT [DATE] FROM #TempTable AS T WHERE T.Personalid=M.Personal_id)
FROM MainTable AS M 
WHERE M.Personal_id IN (SELECT Personalid FROM #TempTable)




SELECT * FROM MainTable


DROP TABLE MainTable

DROP TABLE #TempTable

Comments

1

The problem is that we don't really know what is behind this part of the SQL query.

SELECT 
    t.Date 
FROM 
    TempTable t 
INNER JOIN 
    MainTable m ON t.PersonalId = m.personalid
WHERE 
    t.PersonalId = m.personalid

This part of the query seems to give you more than one value when you try to SET it into the MainTable_Date. You have to give your query more details to get only one value. Maybe you should use a MAX(..) value or try to aggregate your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.