SQL Server will not allow insertion of duplicate column names so Andles "Select *" suggestion will not work if you have duplicate column names in the two tables.
Here is s simple script that creates a "Select Into" query that lists each column of two joined tables. It eliminates columns with duplicate names. You may want to modify it to add a prefix or suffix instead of eliminating the duplicate column names.
Substitute your table names for those in the sample. The output of the script is the SQL "Select Into" query. Copy and paste it into a Query Analyzer Editor window. Run it to create the new table. You can also edit it before running to further refine it.
--------------------------------
Declare @sql1 nvarchar(4000), @sql2 nvarchar(4000)
Set @sql1='Select '
Set @sql2=''
Select @sql1=@sql1+char(10)+' a.'+Column_Name+','
From Utes.information_schema.columns
Where table_name='table1'
Select @sql2=@sql2+char(10)+' b.'+Column_Name+', '
From Utes.information_schema.columns
Where table_name='table2'
And column_name Not In
(Select Column_name
From Utes.information_schema.columns
Where table_name='table1')
Set @sql2=Left(@sql2,len(@sql2)-1)
Set @sql2=@sql2+ char(10) +
'Into NewTable' + char(10) +
'From table1 a' + char(10) +
'Inner Join table2 b' + char(10) +
'On a.key=b.key'
Select @sql1+@sql2 Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.