I think what I am trying to achieve has sort of been asked before in another guise here. But I have a slightly different requirement and more restrictions on what I can do.
In this simplified scenario:
CREATE TABLE backup_data AS SELECT a.*, b.*, c.*
FROM tbl_a a, tbl_b b, tbl_c c
WHERE a.aid = b.bid
AND b.cid = c.cid;
tbl_a, tbl_b and tbl_c have some columns with the same name, which Oracle will return a duplicate column name error for when I try to run it.
What I am hoping for is that there is some way to distinguish the column names so that I can insert them despite having the same name.
I thought this might be possible with a concatenated alias something like:
CREATE TABLE backup_data AS SELECT a.* 'tbl_a_' || COLUMN_NAME, b.* 'tbl_b_' || COLUMN_NAME, c.* 'tbl_c_' || COLUMN_NAME
FROM tbl_a a, tbl_b b, tbl_c c
WHERE a.aid = b.bid
AND b.cid = c.cid;
I fear I already know the only solution will be either:
- Suck it up and write the 617 column names out by hand with appropriate aliases OR
- Use some awesome PLSQL
Well unfortunately I don't have the luxury of PLSQL in this particular instance nor do I really have the luxury of time to do it by hand.
Are there any other ways I can achieve this create as select with duplicate column names?
SELECTstatement can happily return multiple columns with the same name, Oracle can implicitly provide an appropriate alias for the second column. I assume that the requirement for unique aliases is coming from whatever you're doing with the results of yourselect. You're showing aninsert as selectwhich isn't valid SQL syntax-- perhaps you're really doing aCREATE TABLE AS SELECT?