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 2000 Forums
 Transact-SQL (2000)
 Updating Null values

Author  Topic 

scelamko
Constraint Violating Yak Guru

309 Posts

Posted - 2006-07-20 : 16:45:21
Guys,

I have 2 columns in my table

id name
_____________
1 amy
1 null
1 null
1 null
1 null
1 null
1 null
1 null
2 John
2 null
2 null

In this case if any of the rows with same id has not null value than all the same ids should be populated as

id name
_____________
1 amy
1 amy
1 amy
1 amy
1 amy
1 amy
1 amy
1 amy
2 John
2 john
2 john

Is there a way to populate the table in this way

any suggestions/inputs would help

Thanks

DMWCincy
Starting Member

11 Posts

Posted - 2006-07-20 : 16:56:40
Like this?



create table test(id int, name varchar(10))

insert into test
(id, name)
values
(1, 'amy')
insert into test
(id, name)
values
(1, null)
insert into test
(id, name)
values
(1, null)

insert into test
(id, name)
values
(2, 'tim')
insert into test
(id, name)
values
(2, null)
insert into test
(id, name)
values
(2, null)

update t1 set t1.name = t2.name from test t1 inner join test t2 on
t1.id = t2.id and t1.name is null and t2.name is not null
select * from test
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-21 : 11:40:15
or

Update t set name=(select max(name) from table where id=T.id) from table T

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -