views:

379

answers:

5

Hi!

SQL Server question. When doing

INSERT INTO T1 SELECT (C1, C2) FROM T2

I don't want to specify column names of T1 because they are the same as in T2

Is it possible to do so?

Currently I'm getting error

Msg 213, Level 16, State 1, Line 1

Column name or number of supplied values does not match table definition.

A: 

Why not simply

INSERT INTO t1
SELECT * FROM T2
Sparky
I need to skip one column (the one which is identity primary key)
Konstantin Spirin
+3  A: 

Yes, you can omit the field names for the table that you insert to, and you can use select * to get all fields from the table, but I would not recommend this approach.

If you omit the field name the fields are matched by position, not by name. If the fields are not in the exact same order, they will be mixed up. Generally you should avoid relying on the exact layout of the tables, to minimise the risk that changes in the tables breaks the queries.

Guffa
Is there any way to insert all columns except the `identity key` without specifying all column names? Tables I work with contain dozens of columns and listing them every time is really boring.
Konstantin Spirin
Maybe you could create a view with all the columns you want and select * from it.
cindi
@Konstantin: No, there is no simple way to specify all fields except the identity field. It's all or nothing.
Guffa
A: 

If T1 and T2 match exactly you have two choices. You can either select all columns from T2 for the insert into T1, or you can provide a column list to the insert statement.

Even though when you do a select MSSQL provides column headers that information is not used by an insert statement to match columns up.

Donnie
+3  A: 

Always use explicit columns both in the INSERT and in the SELECT projection. Even if you don't want to, you should:

INSERT INTO T1 (C1, c2)
SELECT C1, C2 FROM T2
Remus Rusanu
That's code duplication I want to get rid of in favour of convention-driven approach (columns with same names should just map to each other)
Konstantin Spirin
No, that is good SQL practice.
Remus Rusanu
Code duplication should not be good practice.
Konstantin Spirin
SQL is generally a verbose language. For Example the group by clause is redundant in aggregatations, INTO is redundant. I think you just have to accept that.
cindi
This is a good practice because database systems cannot track dependencies in client code. One can add a column to T2, or it can reorganize T2 to change column order, or it can remove a column, all operations resulting in breaking your code.
Remus Rusanu
A: 

If you're worried about column names you can always alias them:

INSERT INTO T1 (C1, c2)
SELECT C1 AS C1_ALIAS, C2 AS C2_ALIAS FROM T2

Or, more succinctly:

INSERT INTO T1 (C1, c2)
SELECT C1 C1_ALIAS, C2 C2_ALIAS FROM T2

Though I can't really think why one would want to in such a simple example

pygorex1
Is it possible to have alias for a number of columns? For example, `insert into T1 ALL_COLUMNS_ALIAS select (C1, C2, C3) as ALL_COLUMNS_ALIAS from T2` ?
Konstantin Spirin