How to export Oracle table to tab separated values?

I need to export a table to a database to a tab delimited file. I am using DBI on Perl and SQLPlus. Does it support (DBI or SQLPlus) export and import of TSV files or from them?

I can write the code that I need, but I would like to use a ready-made solution, if available.

+3
source share
4 answers

It should be relatively simple to dump the table into a file with values ​​separated by tabs.

For instance:

open(my $outputFile, '>', 'myTable.tsv');

my $sth = $dbh->prepare('SELECT * FROM myTable');

$sth->execute;

while (my $row = $sth->fetchrow_arrayref) {
    print $outputFile join("\t", @$row) . "\n";
}

close $outputFile;
$sth->finish;

Please note that this will not work if your data contains either a tab or a new line.

+2
source

, DBI Oracle ( sqlplus).

, , " yasql" SQLplus) DBD:: Oracle oracle.

yasql , select sql CSV ( Text:: CSV_XS).

, script DBD::Oracle Text::CSV_XS. , , , , :

$csv->print ($fh, $_) for @{$sth->fetchrow_array};

, $csv ​​ . . Text::CSV_XS.

+1

... perl script, , , sqlplus. :

open(UNLOAD, "> $file");      # Open the unload file.
$query =~ s/;$//;             # Remove any trailng semicolons.
                              # Build the sql statement.
$cmd = "echo \"SET HEAD OFF
             SET FEED OFF
             SET COLSEP \|
             SET LINES 32767
             SET PAGES 0
             $query;
             exit;
             \" |sqlplus -s $DB_U/$DB_P";

@array = `$cmd`;              # Execute the sql and store
                              # the returned data  in "array".
print $cmd . "\n";
clean(@array);                # Remove any non-necessary whitespace.
                              # This is a method to remove random non needed characters
                              # from the array

foreach $x (@array)           # Print each line of the
{                             # array to the unload file.
   print UNLOAD "$x\|\n";
}

close UNLOAD;                 # Close the unload file.

, , deliteted pipe... , \t | .

0

awk sqlplus. awk script / oneliner. HTML, .

Save this script as sqlplus2tsv.awk:

# This requires you to use the -M "HTML ON" option for sqlplus, eg:
#   sqlplus -S -M "HTML ON" user@sid @script | awk -f sqlplus2tsv.awk
#
# You can also use the "set markup html on" command in your sql script
#
# Outputs tab delimited records, one per line, without column names.
# Fields are URI encoded.
#
# You can also use the oneliner
#   awk '/^<tr/{l=f=""}/^<\/tr>/&&l{print l}/^<\/td>/{a=0}a{l=l$0}/^<td/{l=l f;f="\t";a=1}'
# if you don't want to store a script file

# Start of a record
/^<tr/ {
  l=f=""
}
# End of a record
/^<\/tr>/ && l {
  print l
}
# End of a field
/^<\/td>/ {
  a=0
}
# Field value
# Not sure how multiline content is output
a {
  l=l $0
}
# Start of a field
/^<td/ {
  l=l f
  f="\t"
  a=1
}

Did not test this with long lines and strange characters, it worked for my use case. An enterprising soul could adapt this method to the perl shell :)

0
source

Source: https://habr.com/ru/post/1727459/


All Articles