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
 INSERT INTO problem

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-07-19 : 11:36:25
Hi All - I'm currently using SSMS 2008.

I'm working with the two following tables (simplified, of course) with the corresponding column names:
[CODE]
TABLE_1_CUMULATIVE
Column_A
Column_B
Column_C
Column_D

TABLE_2_CURRENT_MONTH
COLUMN_B
COLUMN_A
COLUMN_C
COLUMN_D
[/CODE]
I need to insert the records from the Current_Month table to the Cumulative table. The column names align, it's just that the physical location of the columns do not.

I was under the impression that if I specified the source and destination columns right beside each other, I could get away with the following INSERT into Statement:

[CODE]

INSERT INTO TABLE_1_CUMULATIVE
SELECT

[Column_A] [[Column_A]
,[Column_B] [Column_B]
,[Column_C] [Column_C]
,[Column_D][Column_D]

FROM TABLE_2_CURRENT_MONTH

[/CODE]

However it keeps inserting column_b values into Column A and so forth.
The actual table has about 45 columns, so it'd be lots of work to manually align the columns.

Is there any way you can make the above Syntax actually work?
Thanks!!!!

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-19 : 11:44:09
The column list defines the order.

INSERT INTO TABLE_1_CUMULATIVE
(
Column_A,Column_A,Column_C,Column_D)
)
SELECT

[Column_A] [[Column_A]
,[Column_B] [Column_B]
,[Column_C] [Column_C]
,[Column_D][Column_D]

FROM TABLE_2_CURRENT_MONTH



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-19 : 13:07:57
quote:
Originally posted by nigelrivett

The column list defines the order.

INSERT INTO TABLE_1_CUMULATIVE
(
Column_A,Column_AColumn_B,Column_C,Column_D)
)
SELECT

[Column_A] [[Column_A]
,[Column_B] [Column_B]
,[Column_C] [Column_C]
,[Column_D][Column_D]

FROM TABLE_2_CURRENT_MONTH



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.


small typo

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -