Eric Heinrichs Eric Heinrichs - 8 months ago 36
SQL Question

SQL Server : copying columns to another table based on a join

I am pretty inexperienced when it comes to SQL, so I apologize if this is a rookie question.

I need to add columns to a table in a database, based on a join from another database, using a common column between the two tables.


When you say "add columns to a table in a database," do you mean that you have a table, e.g.

create table Targets (
    TargetID int identity(1,1) not null ,
        constraint pkc_Target primary key clustered ( TargetID ) ,
    TargetField1 varchar(64) not null ,
    ..... )

And you want to add additional columns to that table? If so, you'd do something like

alter table dbo.Targets add TargetNewField1 varchar(64) null

After that, you'd have empty columns in your table, and could then run an update to fill in the blanks, something like:

update dbo.Targets
set dbo.Targets.TargetNewField1 = dbo.Source.SourceField1
from dbo.Targets
inner join dbo.Source
on dbo.Targets.SomeUniqueField = dbo.Source.SomeUniqueField