Insert into with union all and nextval doesn't work with duplicate values

喜你入骨 提交于 2019-12-11 06:28:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!