mevr
mevr

Reputation: 1125

Update column of one table from same table

I need to update one column of table with other from same table

UPDATE table 
SET table1.name = table1.nickname
FROM table 
WHERE userid = 5

Is there any problem in my query, please help.

Upvotes: 2

Views: 114

Answers (3)

Fernando Rodriguez
Fernando Rodriguez

Reputation: 493

Looks like you're adding some syntax you don't need...if all the data you need is already there in that table, simply do like so, regardless of which flavor of SQL (I think; I could be wrong on that part):

UPDATE dbo.tablename SET columnToChange = columnWithCorrectValue WHERE userid = 5

Upvotes: 3

Ram Singh
Ram Singh

Reputation: 6918

two things your are doing wrong:

  1. Update query is wrong, i means syntax
  2. Second, you are trying to update table1 instead of table

your query should be:

UPDATE table
SET table.name = table.nickname
WHERE table.userid = 5

OR

UPDATE table
SET name = nickname
WHERE table.userid = 5

for more info regarding the syntax for update in sql server, please check this

Upvotes: 1

Wajih
Wajih

Reputation: 4383

No, you just need to remove From table part from your syntax.

Upvotes: 1

Related Questions