Dynamic Select Statement

I need to write a dynamic Select statement to copy data from all the tables in source database to target db tables.


INSERT INTO SRC_TAB_1 (col1, col 2, col3) SELECT col1, col2, col 3 FROM TRG_TAB_1;
INSERT INTO SRC_TAB_2 (col1, col 2, col3) SELECT col1, col2, col 3 FROM TRG_TAB_2;

I have few additional columns in all the target tables which also needs to be populated with default values.

E.g. BATCH_ID = 100, DATE = SYSDATE etc.

Please can anyone advise how to write a SELECT query to copy from 1 table to another dynamically along with populating additional columns as described above.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Just make them literals?

INSERT INTO SRC_TAB_1 (col1, col 2, col3, batch_col, date_col) SELECT col1, col2, col 3, 100, sysdate FROM TRG_TAB_1;

To make it 'dynamic', you can use variables.  How you create variables depends on the tool you are using.

In sqlplus it is:
variable batch_id number
exec :batch_id := 100;

insert ... select :batch_id, ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
Without being able to see the table structures of both the source and target tables, it's a little difficult to determine how the dynamic sql should be constructed. Also, would this include tables from a single schema or selected schemas or all schemas? Would all of the target tables have the same default values with the same column names? A simple example of dynamic sql that copies data from some tables to other tables might take the following form:
v_sql varchar2(4000);
v_tgt varchar2(30);
for t in (select table_name from user_tables where table_name in (src_tab1, src_tab2) loop
v_tgt := replace(t.table_name,'src','tgt');
v_sql := 'insert into '||v_tgt||' select a.*, 100 as batch_id, sysdate as sdate from '||t.table_name||' a';
execute immediate v_sql;
end loop;
The above would require a lot of unlikely assumptions such as the table structures (i.e. column order) and names would have to be identical except for the src and tgt reference and for the two default target columns which would have to be at the end. I think we need a lot more detail before we develop a solution for your case.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.