Is it possible to specify an alias name for the table I am inserting values into?
I want to specify a condition inside a nested query and the table is too verbose…
Something like turning this:
INSERT INTO my_table_with_a_very_long_name (col_a, col_b, col_c)
SELECT foo, bar, baz
FROM other_table
WHERE
other_table.some_value >
(SELECT max(other_value) FROM my_table_with_a_very_long_name);
into this:
INSERT INTO my_table_with_a_very_long_name AS t (col_a, col_b, col_c)
SELECT foo, bar, baz
FROM other_table
WHERE
other_table.some_value > (SELECT max(other_value) FROM t);
(obviously my case is longer and involves a few references more)
You don’t alias a table, you alias an instance of a table reference.
This allows self joins, etc as you have mutliple instances of references to the same physical table. It’s not a case where each
ASgives that table a new name elsewhere, it’s just an alias to refer to That particular reference.In your case, there are two show stoppers…
The table being inserted into isn’t itself part of the select query, it’s not a referenced set in the same way as
foo,barorbazfor example. So, you can’t alias it at all (because there’s no need, it can never be referenced).Also, even if it was, you can’t reference the whole table through an alias. You reference a field, as part the query itterating through the set. For example, this doesn’t work either…
You can get around the latter example using…
But that still brings us back to the first point, the table being inserted into never gets referenced in the query part of your statement.
The only way I can think of getting close is to create a view…