Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update Column From Other Table Column

Author  Topic 

alexjamesbrown
Starting Member

48 Posts

Posted - 2009-01-13 : 05:05:35
Hi, Currently, we have a users table with a schema like:

UserID | UserName
1 | joebloggs
2 | jonsmith
3 | alexjamesbrown

We then have a table (we'll call this Profile for the sake of this..) with a schema like:

User | HairColour | FavouriteFood
1 | Brown | Pizza
2 | Blonde | Pasta
3 | Brown | Chinese

What i need to do is replace the User column in the Profile table with the actual user name, stored in the users table something like...


update Profile set User = (SELECT username from Users where UserID = ???)


this would make the above profile table read:

User | HairColour | FavouriteFood
joebloggs | Brown | Pizza
jonsmith | Blonde | Pasta
alexjamesbrown | Brown | Chinese

Thanks for your help

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-13 : 05:18:05
first alter ur user column in varchar if it is int
then try this
alter table profiles alter column users varchar(32)
update profiles
set users = u.username
from users u
where users = u.userid
Go to Top of Page

alexjamesbrown
Starting Member

48 Posts

Posted - 2009-01-13 : 07:41:48
perfect, worked brilliantly.
cheers.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-13 : 07:44:53
quote:
Originally posted by alexjamesbrown

perfect, worked brilliantly.
cheers.



welcome
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-13 : 08:44:58
why should you do this? isnt it enough to join onto users table and get this information directly in select statements while retrieving the value?
Go to Top of Page
   

- Advertisement -