Does INSERT INTO ... SELECT ... always match fields by ordinal position?

Heinzi picture Heinzi · Mar 21, 2012 · Viewed 8.1k times · Source

My tests seem to confirm that

INSERT INTO a (x, y) SELECT y, x FROM b

maps b.y to a.x, i.e., the fields are matched only by ordinal position and not by name. Is this always the case, i.e., can I rely on that behaviour? Unfortunately, the documentation does not specify this (or I didn't find it).

Answer

Aaron Bertrand picture Aaron Bertrand · Mar 21, 2012

That is correct, SQL Server does not try to do any mapping of column names since you can apply any aliases to the source data that you like. It will always reference ordinal position.