Avoiding to repeat field names when using INSERT INTO ... SELECT
I have a table with a very large number of fields, and the table is being
populated by a INSERT INTO ... SELECT query. Since the number of fields is
so large, I get something like this:
INSERT INTO MyTable (
column1,
colunm2,
colunm3,
...
column200)
SELECT column1,
column2,
column3,
...
column200
FROM SomeView;
The above is cumbersome and error prone to maintain, so I'd prefer if it
was possible to have some kind of "natural insert" where the field names
in the SELECT clause are mapped to the fields with the same names in the
target table.
I tried this:
INSERT INTO MyTable
SELECT column1,
column2,
column3,
...
column200
FROM SomeView;
Which is syntactically correct, but relies on matching the order of the
fields, which is even more error prone, so what to do? Am I overlooking a
much more obvious third option?
Clarification: the purpose is to make it easier to maintain the code in
the long run. We will be updating this statement many times, so I am
looking for a way to make it more readable and easy to modify.
No comments:
Post a Comment