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
 Inserting with different column names

Author  Topic 

gemispence
Yak Posting Veteran

71 Posts

Posted - 2006-02-15 : 10:42:07
I'm trying to perform an insert on a table from another table, but
the fieldnames are different. The data is similar, but there are
less fields as well. Does anyone know how to accomplish this? All
the material I've read on the Internet so far pertaining to Insert
statements, has all the column names matching up.

Any help would be greatly appreciated.

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-02-15 : 10:44:06
Insert Into TableDestination (Filed_A, Field_B, Field_C)
Select Field_X, Field_Y, Field_Z From TableSource

Specify the source & destination Fileds in the proper order
ie.
Field_X data to be inserted to Field_A
Field_Y data to be inserted to Field_B
Field_Z data to be inserted to Field_C

It doesn't matter
TableDestination has more fields as Filed_D, Filed_E ...
TableSource has more fields as Filed_U, Filed_V ...
Field_A name being different from Field_X
as long as the corresponding fields match in data type





Go to Top of Page

gemispence
Yak Posting Veteran

71 Posts

Posted - 2006-02-15 : 10:47:36
Thank you so much for the quick reply :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-16 : 00:58:49
Also note that data types should match with respective columns

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -