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
 SQL Server Administration (2000)
 Update Query!!!

Author  Topic 

samrat
Yak Posting Veteran

94 Posts

Posted - 2002-10-24 : 01:55:50
Greetings,

I m looking for a solution on a update query. Following is a prototype table structure and sample data

Table 1
Field1 Field2
A 1
B 2
C 3
D 4



Table2

Field1 Field2
A NULL
D 4
B NULL
A 1
C 3
D NULL
B NULL


I wanna write a update statement, to update the NULL values in Field2(Table2) as per the corresponding values in Table1.

Regards,


Samrat

Edited by - samrat on 10/24/2002 02:10:03

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2002-10-24 : 05:14:17
I think this whould work.

update table2 set field2 =(select field2 from table1 where table1.field2=table1.field1) and table2.field2 is null

Harsh.

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-10-24 : 06:49:19
I think this whould work better.


update table2
set field2 =(select
field2
from
table1
where table1.field2field1=table1.field1)
andwhere
table2.field2 is null

 
...or I like...

update
table2
set
field2 = b.field2
from
table2 a
inner join table1 b
on a.field1 = b.field1
where
a.field2 is null

 


Jay White
{0}
Go to Top of Page

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2002-10-24 : 07:11:05
ok ya that was my error


Go to Top of Page

samrat
Yak Posting Veteran

94 Posts

Posted - 2002-10-24 : 21:47:36
Thanx guyz.. It works well...

Regards

Samrat
Go to Top of Page
   

- Advertisement -