Avatar of ITBenelux
Flag for Belgium asked on

SQL Update


I don't understand why my update statement is not working

I want to update table tbluser with values of tblDump, comparing the UserID's.  I put a unique index on the userID field in both tables, So I'm 100% sure there are no duplicate values.  yet.. when running the update statement. I get back 'Subquery returned more than 1 value.' ?? which should be impossible. because there is always one value for one ID ?

thx for helping me out on this one.
USE users
UPDATE tbluser
SET PartnerBirthday = (SELECT PBirthday
FROM users.dbo.tblDump
WHERE users.dbo.tblDump.userid  = users.dbo.tbluser.userid)

Open in new window

Microsoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment

8/22/2022 - Mon

try this

USE users
UPDATE users.dbo.tbluser
SET PartnerBirthday = PBirthday
From users.dbo.tblDump
WHERE users.dbo.tblDump.userid  = users.dbo.tbluser.userid

But there is nowhere you are constraining the results of the sub query, so it will return all user records.

SET u.PartnerBirthday = d.PBirthday
FROM users.dbo.tblDump d
JOIN users.dbo.tbluser u
  ON u.userid  = d.userid

Open in new window

Will update all records in tblUser where there is a record in tblDump.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

USE users
UPDATE users.dbo.tbluser
SET PartnerBirthday = PBirthday
From users.dbo.tbluser  inner join  users.dbo.tblDump
 on users.dbo.tblDump.userid  = users.dbo.tbluser.userid
Your help has saved me hundreds of hours of internet surfing.

UPDATE table2  SET table2.col1 = table1.col1,  table2.col2 = table1.col2
FROM table1, table2  
WHERE table1.id = table2.id

It seems this answer helped me out. In the end I was wrong, running the query showed me that there are duplicated values. however I was sure to put the index correctly.  So it's back to the drawing table. but this helped me on the right track.