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
 General SQL Server Forums
 New to SQL Server Programming
 Update table

Author  Topic 

VeselaApo
Posting Yak Master

114 Posts

Posted - 2011-01-19 : 11:43:34
Hi,

I have two tables with data. Table 1 has a list of all student IDs a blank column "Registration Status". The table looks like that:

StudentID Registration Status
001, Null
002, Null
003, Null
004, Null
005, Null
...
998, Null

The second table lists StudentIDs but NOT ALL of them and has the status of their registration as shown below:

Student ID Status
005, Enrolled
046, Enrolled
300, Enrolled

I would like to update the Registration Status Column in Table 1 with the status Enrolled from Table 2 on all those students that T1.StudentID = t2.StudentID. The statement i tried is below but it does not work. Any suggestions?

update T1
set Registrationstatus = (select Status from T2 where T1.StudentID = t2.StudentID)
where T1.StudentID = t2.StudentID


Thanks!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-01-19 : 12:30:14
update t1
set Registrationstatus = t2.Status
from table1 as t1
join table2 as t2 on t1.StudentId = t2.StudentId



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -