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

It seems that DBD:Oracle cant read in columns that have spaces in between. For example the field that this error is reporting is a string of '99602BOB BO3606'. '99602BOB BO3606' is from a VarChar field in SQL Server and going into a Char field in Oracle. It seems to think that it needs commas in the value. I would think that since its being stored as a string in an array that this shouldnt be a problem. So why would Oracle complain about this? The error accurs during the insertion into Oracle database during the while loop($row[0] is '99602BOB BO3606' and thats where it's erroring out). Here is a copy of my code:
#!/usr/bin/perl use DBI qw(:sql_types); use DBD::Oracle qw(:ora_types); # setting up database connection to SQL Server $dblab = DBI->connect('dbi:ODBC:DL_SWIPE','user', 'pass', { RaiseErro +r => 1, odbc_cursortype => 2}); # setting up database connection to CDW-V my $dbv = DBI->connect( 'dbi:Oracle:cdwv','user','pass',) || die "Database connection not made: $DBI::errstr"; #Prepare SQL to create Table $table = $dbv->prepare("Create table DL_SWIPE (CR_APLT_I char, FRST_N +char, MID_INIL_N char, LAST_N char, SSN_I char, ID_I char, ISS_ST_C c +har, BIRTH_D char, ADDR_LINE_1_T char, ADDR_LINE_2_T char, CITY_N cha +r, ST_C char, PSTL_C char, ID_ISS_D char, ID_EXPR_D char, ID_SCAN_MET +H_C char, ID_REC_CHG_F char, ID_SYS_RESP_C char)"); # # Execute create Table stmt $table->execute( ); ### Prepare a SQL statement for execution $sth = $dblab->prepare( "SELECT * FROM CR_APLT_ID_CD"); ### Execute the statement in the database $sth->execute; ### Retrieve the returned rows of data while ( @row = $sth->fetchrow_array( ) ) { print @row; ### Errors out here, $row[0] where the scalar = '99602BOB BO3606' $inserted = $dbv->prepare("INSERT INTO DL_SWIPE VALUES ($row[0], $row[1], $row[2],$row[3],$row[4], $row[5],$row[6],$row[7], $row[8],$row[9],$row[10],$row[11],$row[12],$row[13],$row[14],$row[15 +],$row[16], $row[17],$row[18])"); $inserted->execute(); } ### Disconnect from the database $dblab->disconnect or warn "Error disconnecting: $DBI::errstr\n"; exit; # ### Disconnect from the database $dbv->disconnect or warn "Error disconnecting: $DBI::errstr\n"; exit;
Thanks in advance, Fuism

Replies are listed 'Best First'.
Re: DBD:Oracle::db prepared error
by Joost (Canon) on Aug 19, 2004 at 21:09 UTC
    You need to quote your strings. Either use $dbh->quote() method or use binding:
    $sth = $dbv->prepare("INSERT INTO DL_SWIPE VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); $sth->execute($row[0], $row[1], $row[2],$row[3],$row[4],$row[5],$row +[6],$row[7], $row[8],$row[9],$row[10],$row[11],$row[12],$row[13],$row[14],$row[15], +$row[16], $row[17],$row[18])"); # update: the line above can be changed to # $sth->execute(@row); # if @row has exactly 18 elements
    see the DBI documentation for more info.

    Also, you'll be in a ton of sh*t if the order of your columns ever changes. It's probably a good idea to name your columns in that prepare() statement.

    Joost

      my $qmark = join(',', ('?') x @row); $sth = $dbv->prepare( "INSERT INTO DL_SWIPE VALUES( $qmark )" ); $sth->execute( @row );

      Much easier to read and maintain. You knew that ...

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

        $dbv->do("INSERT INTO DL_SWIPE VALUES( $qmark )",undef,@row);
        You knew that...

        rdfield

      Just to add a little more detail to Joost's comment:

      create an array of column names (if columnnames in DB1 eq columnnames in DB2) , then use that array to build the select and load statements.

      If columnnames in DB1 ne columnnames in DB2 then use a hash to show the correspondance and build the select and load statements from the keys/values of the hash.

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday