问题
I want to insert 2 lines in a table within one insert into
statement with Oracle SQL.
This code works:
insert into a_glw select tt.*, work_id_seq.nextval from
(select 11111, 'one text', 12345, 'new text', NULL,
'some text', 'nice text', 'test', 'text', 'great text'
from dual
union all
select 11111, 'one text', 12345, 'new text', NULL,
'some text', 'nice text', 'test', 'text', 'great text'
from dual) tt;
When I change the value test
to text
this code produces the error 00918. 00000 - "column ambiguously defined"
:
insert into a_glw select tt.*, work_id_seq.nextval from
(select 11111, 'one text', 12345, 'new text', NULL,
'some text', 'nice text', 'text', 'text', 'great text'
from dual
union all
select 11111, 'one text', 12345, 'new text', NULL,
'some text', 'nice text', 'test', 'text', 'great text'
from dual) tt;
It seems to be a problem to insert identical values in one select statement. How can I fix this?
回答1:
As the values are different in the second example, you have to have an alias name for your columns in order to execute the insert statement.
In the first example, test
is the column value and it assumes test
as the default column name as you did not provide alias name.
See the example here
If you look at the enclosed screenshot, the second example is having TEXT columns repeated twice as the select statement is considering the column value as the column name and therefore you must provide alias names for the columns.
来源:https://stackoverflow.com/questions/43743968/insert-into-with-union-all-and-nextval-doesnt-work-with-duplicate-values