Skip to Main Content
  • Questions
  • Export query output to one file and log to another file

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Vanka.

Asked: October 13, 2016 - 5:13 am UTC

Last updated: October 14, 2016 - 11:54 am UTC

Version: Oracl12c Linux

Viewed 1000+ times

You Asked

How to export Sql query results(only rows) to one table and log(Number of rows selected) to another table.

and Chris said...

Great reasons to use PL/SQL, number 4,326:

You can access the number of rows affected by the previous DML statement using the SQL%ROWCOUNT attribute!

So you can do your export as a "insert into ... select". Then get the count from sql%rowcount:

create table t as
  select rownum x from dual connect by level <= 1000;
  
create table t_export (
  x int
);

create table log_count (
  row_count int
);
  
declare
  rws pls_integer;
begin
  insert into t_export
    select x from t
    where  x <= 100;
    
  rws := sql%rowcount;
  
  insert into log_count values ( rws ); 
end;
/

select count(*) from t_export;

COUNT(*)  
100     

select * from log_count;

ROW_COUNT  
100 


Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Vanka Satyanarayana, October 14, 2016 - 7:20 am UTC


Vanka Satyanarayana, October 14, 2016 - 7:24 am UTC

Thank you for your response. Let me put in this way, in my project we have to export the query results to the file and same file is should be imported to another table. We were doing this in db2 earlier. There we have simple command like export and Import but not sure how we proceed with Oracle. Please let me know if you have any thoughts on this.
Connor McDonald
October 14, 2016 - 11:54 am UTC

export the query results to the file and same file is should be imported to another table

Why do you need the file? What's wrong with "insert ... select ..."?

If you're talking about copying to a different database, then import and export exist in Oracle too!

If so look into the query parameter of data pump. This allows you to filter the rows as needed:

https://oracle-base.com/articles/10g/oracle-data-pump-10g#content-and-query