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 row from another row in a table

Author  Topic 

rookie_sql
Constraint Violating Yak Guru

443 Posts

Posted - 2008-04-11 : 06:39:17
Hi i have 2 rows of data in a table i'd like to update a column in one of the rows based on the other row.

my data set is like this. I'd like to update the der_code * with the value of 10.


prd_code der_code
1000 10
1000 *

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-04-11 : 06:45:17
[code]declare @t table
(
prd_code int,
der_code varchar(10)
)

insert @t
select 1000, '10' union all
select 1000, '*'

update t1
set der_code = t2.der_code
from @t t1 join @t t2 on t1.prd_code = t2.prd_code
where t1.der_code <> t2.der_code and t1.der_code = '*'

select * from @t[/code]

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

- Advertisement -