views:

18

answers:

2

what if I wanted to update the records in the table by altering values in one of the columns?

I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.

Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.

+1  A: 

See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.

Darvis Lombardo
tried different combinations. not getting it right
vbNewbie
+1  A: 

For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.

Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...

UPDATE old SET old.badColumn = new.newData
FROM oldTable old 
JOIN newTable new on old.someID = new.someID

This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.

Fosco
The column that has null values was accidently altered to null; it is related to the other columns in each record therefore I am trying to insert the original data back into that column; but cannot get it right. The only thing is that it inserts at the end of the table.
vbNewbie
You need to do something like this... UPDATE a set a.badColumn = b.newData from myTable a join otherTable b on a.someid = b.someidDoes this make sense? :)
Fosco
thanks let me try that quick
vbNewbie
Thanks..one issue, errorthe multi-part identifier new.newData could not be bound
vbNewbie
ok sorry never mind. it worked. Thanks againI had newTable.newData instead of new.newData
vbNewbie

related questions