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)
 Insert the value of another field

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-10-05 : 05:02:34
Hi,

I want to loop through a table and update the value of field 1 with the value of field 2 in the same row. How can I do that?

Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-10-05 : 05:05:04
[code]Update Table
set Field1 = Field2
Where <some_condition>[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-10-05 : 05:43:07
Sorry got myself confused with this, field 2 is in a seperate table. So what I need to do is:

Update Table1 SET Field1 = (SELECT Field2 FROM Table2 WHERE Table2.id = Table1.id)
WHERE Table1.Make = "BMW"

Is that statement correct?

Thanks
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-10-05 : 05:49:15
Or still better:

Update t1
Set Field1 = t2.Field2
From Table t1 join Table2 t2 on t1.id = t2.id
where t1.Make = 'BMW'


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -