Link to home
Start Free TrialLog in
Avatar of Rao_S
Rao_S

asked on

How to use the "using" clause with "insert"?

hi,
i want to insert rows from one table into another - with the using clause like so...
insert into table1 using select * from table2;
table1 has the same structure as table2 except one extra column in the end which is nulable, so the above insert will not work..
if the strucute is exactly same then i have no problem with the above insert statement.
is there a easy way to insert into table1 without listing all the columns?
Avatar of Rindbaek
Rindbaek
Flag of Denmark image

You can do it this way
create a staging table temp_t2 as a select * from t2 where whatever...
Then alter the table temp_t2 adding the extra column from t1;
Then insert  from temp_t2 into t1.
And finally drop table temp_t2
Avatar of Rao_S
Rao_S

ASKER

i know, i can do it that way.. i was hoping there was one swell swoop of inserting data with just one statement...
Avatar of Alex [***Alex140181***]
insert into table1 select a.col1, a.col2 ...., null from table2 a;

btw:
select 'a.' || to_lower(a.column_name) || ','
from user_tab_columns a
where a.table_name = 'table2'
order by a.column_id;

Open in new window


will generate your base select statement. all you have is to add the missing null value for the "new" col of table1...
ASKER CERTIFIED SOLUTION
Avatar of flow01
flow01
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rao_S

ASKER

thank you flow01! it worked!
elegant...like it flow01
nice points distribution...
Avatar of Rao_S

ASKER

hi Alex140181, i wanted to distribute the point to all answers, but i remember, that once i was told to award points to the correct and best answer only...
Points should go the the correct answer and assist could go to answers that helps clarify something or provides part of the answer. In this case Flow01 have provided the easiest and most elegant solution. So i agree with the point distribution (and learned a bit on the way) ;-)