http://www.perlmonks.org?node_id=226582

rbc has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have am writing a script that will create sqlldr control
files for me. I am having trouble printing 0.
I am sure this a common problem that you wise monks have
seen before and have overcome with easy.
Here's my script:
#!/usr/bin/perl -w use strict; use DBI; my $table = shift; my $dbuser = shift; my $dbpass = shift; my $dbname = shift; my $dbd = 'Oracle'; my $dbh = DBI->connect ( $dbname, $dbuser, $dbpass, $dbd); if (!$dbh) { die "$0 Error conecting to DB ($dbuser/$dbpass\@$dbname); $DBI::er +rstr\n"; } my $SQL = <<SQL; Select * from $table SQL $dbh->{LongReadLen} = 65500; my $sth = $dbh->prepare($SQL) || die $dbh->errstr; $sth->execute(); my $colNames = $sth->{'NAME'}; my $nColumns = $sth->{'NUM_OF_FIELDS'}; print <<DOIT; OPTIONS (ROWS=1) LOAD DATA INFILE * REPLACE INTO TABLE $table FIELDS TERMINATED BY '|' TRAILING NULLCOLS ( DOIT my $colString; for my $colName ( @$colNames ) { $colString .= "\n\t$colName,"; } chop($colString); print "$colString\n"; print<<DOIT; ) BEGINDATA DOIT while ( my @r = $sth->fetchrow_array ) { # print join ( '|', @r ); # This causes another bug of putting 0's where # I wanted || outputted :( # print join ( '|', map ( $_ || '0', trim( @r )) ); # print join ( '|', map ( $_, trim( @r )) ); print "\n"; } $sth->finish || die; $dbh->disconnect; sub trim { my @out = @_; if ( !defined(@_)) {return "";} if ( $#out == -1 ) {return "";} for(@out) { #next if !($_); if (!($_)) { $_ = ""; } s/^\s+//; s/\s+$//; s/\|//g; s/\n//g; s/\cM//g; s/\r//g; } return wantarray ? @out : $out[0]; }
And here is a example of the bug:
OPTIONS (ROWS=1)
LOAD DATA 
INFILE *
REPLACE INTO TABLE DECODE_TABLE
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(

        VALUE,
        DESCRIPTION
)
BEGINDATA
1|Something
2|Something else
3|Sometimes
4|Never
5|Always
6|Only once in a while
7|Once in a bluemoon
|All the time
Note the line ...

|All the time

... should of been outputted as ...

0|All the time

As per this query below ...
$ sqlplus blah...

SQL> select * from DVAL_ADR_AVAIL
  2  /

V DESCRIPTION
- ------------------------------
1 Something
2 Something else
3 Sometimes
4 Never
5 Always
6 Only once in a while
7 Once in a bluemoon
0 All the time

8 rows selected.
Please help! Thanks :)