tags:

views:

67

answers:

3

Is there a way to name or guide your values in an insert statement? eg.

declare @temp TABLE
(
    column1 int,
    column2 int,
    column3 int
)

insert @temp(column1, column2, column3)
Select 1 as column1,3 as column3, 2 as column2

select * from @temp

will return a 3 in column2 when I would like the 2 in column2 and 3 in column3.

EDIT

I am trying to write some dynamic sql mapping code. Different columns map to our data from each vendor. So my plan was to write the insert statement to go and grab all the vendor columns from a mapping table. However, I cannot be sure the columns will come out of the mapping table in the correct order.

+1  A: 

Why not just rename the columns? Maybe I am missing something?

INSERT INTO table
(column-1, column-2, ... column-n)
VALUES
(value-1, value-2, ... value-n);

Allows you to specify the column name and matching values in that same order.

Edit The order of the columns coming out is precisely in the order that you specify in the select clause. Again I am missing something very big.

ojblass
+1  A: 

insert into mytable(col1, col3, col2) values (:val1, :val3, :val2)

Arnshea
+1  A: 

Different columns map to our data from each vendor.

Write a view per vendor that maps each vendor's tables to your common format.

Do your inserts from each view, or a union of the views.

tpdi