Skip to Main Content
  • Questions
  • How to dump table content into sql file with a list of INSERT statements?

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Jenny.

Asked: January 20, 2006 - 11:37 am UTC

Last updated: January 25, 2006 - 4:33 pm UTC

Version: 9.2.0.1

Viewed 1000+ times

You Asked

Hi Tom,

When import from dump file, we can create a sql script(create_hugo_table. sql) with the CREATE and ALTER TABLE SQL statements by using the following command:

imp userid=systey
fromuser=scott touser=hugo
file=exp_scott.dmp log=imp_hugo_table.log
rows=n indexes=n
indexfile=create_hugo_table.sql

How can I dump table contents into sql file with a list of insert statements like "INSERT INTO tablename (columns) VALUES(......)'?

Thank you very for your time!

Best Regards,

Jenny

and Tom said...

you cannot.

You would have to write a program to do that.

we were just playing around with that recently:

</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:2151576678914#55132817721765 <code>



Rating

  (2 ratings)

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

Comments

Raptor

A reader, January 20, 2006 - 1:44 pm UTC

The Raptor early adoptor release can export INSERT statements.
Right click the table, Export->SQL Insert will generate insert statements into a file.
I don't know if I can incorporate it in a script though.

Tom Kyte
January 20, 2006 - 2:47 pm UTC

neat, learned two things today :)

that makes it a good day.

How to pass the dbms_sql.column_value(my_Cursor, 1, col_value ) as a varchar2 parameter?

Jenny, January 25, 2006 - 1:16 pm UTC

Hi Tom,

The dump_table_to_csv() works very well. now I wrote a procedure dump_all_tables()to dump all contents of user_tables into a sql file. but I got errors as the following:

ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got SYS.AQ$_SUBSCRIBERS
ORA-06512: at "SYSTEM.DUMP_TABLE_TO_CSV", line 62
ORA-06512: at "SYSTEM.DUMP_ALL_TABLES", line 23
ORA-06512: at line 1

bellow is my procedure:
create or replace procedure dump_all_tables(p_dir in varchar2,
p_filename in varchar2)
is
my_Cursor integer default dbms_sql.open_cursor;
my_query varchar2(1000)
default 'select table_name from user_tables ';
col_count number := 0;
col_value varchar2(30);
my_status integer;

table_name varchar2(30);
my_descTbl dbms_sql.desc_tab;
begin
dbms_sql.parse( my_Cursor, my_query, dbms_sql.native );
dbms_sql.describe_columns(my_Cursor, col_count, my_descTbl );
dbms_sql.define_column(my_Cursor, 1, col_value, 4000 );
my_status := dbms_sql.execute(my_Cursor);

while(dbms_sql.fetch_rows(my_Cursor)> 0)
loop
dbms_sql.column_value(my_Cursor, 1, col_value );
table_name := to_char(col_value);
dump_table_to_csv(table_name, p_dir,p_filename );
end loop;
end;
/

Please guide me why I can not pass the col_value as a varchar2 parameter when calling dump_table_to_csv()?
How can I do to make it works?

Thanks for your time.

Jenny

Tom Kyte
January 25, 2006 - 4:33 pm UTC

the problem is, dump csv works only with "basic" datatypes - strings, numbers, dates, raws (which will be in HEX).

complex object types - you cannot do that with dbms_sql.