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

This has been driving me crazy for days.
The below SQL string when printed and pasted into SQL Plus works lovely,
yet fails when attempted to run via PERL.
Oh yeah and it fails quietly, no errors, no warnings.

I tried this:

my $query = <<"SQL"; insert into swma_temp_table (m_serial,m_start,m_end,m_stat,m_product) select '$serialno',to_date('$row->[5]', 'YYYY-MM-DD'),to_date('$row->[ +6]', 'YYYY-MM-DD'),'$row->[17]','$row->[9]' from dual where exists (select NULL from servers where host_serial='$serialno') SQL my $rv = $dbh->do($query) or die "prepare failed: " . $dbh->errstr(); #print $query.";\n -- \n";

I also tried this:

my $query = <<"SQL"; insert into swma_temp_table (m_serial,m_start,m_end,m_stat,m_product) select '$serialno',to_date('$row->[5]', 'YYYY-MM-DD'),to_date('$row->[ +6]', 'YYYY-MM-DD'),'$row->[17]','$row->[9]' from dual where exists (select NULL from servers where host_serial='$serialno') SQL my $stmt = $dbh->prepare($query) or die "prepare failed: " . $dbh->errstr(); $stmt->execute() or die "That serial number is not in our database +: " . $stmt->errstr(); #print $query.";\n -- \n";

if your curious here is my connect statement:

my $dbh = DBI->connect("dbi:Oracle:host=superdb;sid=ora46t;port=1522", + 'nunya','beezwax') or die ("connect failed: " . $DBI::errstr. "\n"); $dbh->{AutoCommit} = 1; $dbh->{PrintError} = 1; $dbh->{RaiseError} = 0; $dbh->{ora_check_sql} = 1; $dbh->{RowCacheSize} = 16;

Thanks

The variables in the above query get populated from a CSV file.
Here is some sample output when I enable the print command:

insert into swma_temp_table (m_serial,m_start,m_end,m_stat,m_product) select '10-1D48H',to_date('2009-09-21', 'YYYY-MM-DD'),to_date('2011-07 +-31', 'YYYY-MM-DD'),'Active','694275P' from dual where exists (select NULL from servers where host_serial='10-1D48H') ; -- insert into swma_temp_table (m_serial,m_start,m_end,m_stat,m_product) select '10-2D65H',to_date('2008-04-25', 'YYYY-MM-DD'),to_date('2011-07 +-31', 'YYYY-MM-DD'),'Active','694273H' from dual where exists (select NULL from servers where host_serial='10-2D65H') ; -- insert into swma_temp_table (m_serial,m_start,m_end,m_stat,m_product) select '10-2D65H',to_date('2008-04-25', 'YYYY-MM-DD'),to_date('2011-07 +-31', 'YYYY-MM-DD'),'Active','694275O' from dual where exists (select NULL from servers where host_serial='10-2D65H') ;

Replies are listed 'Best First'.
Re: Oracle Insert with DBI
by SilasTheMonk (Chaplain) on Jan 12, 2010 at 14:51 UTC
    What do $row->[?] resolve to? Could you show us what the SQL actually looks like when resolved?
      I have updated the original post
Re: Oracle Insert with DBI
by almut (Canon) on Jan 12, 2010 at 16:41 UTC

    In case there are no errors, TRACING sometimes helps.

      I see this in the trace output:
      -> prepare for DBD::Oracle::db (DBI::db=HASH(0x9b82ad8)~0x9b82b20 'in +sert into swma_temp_table (m_serial,m_start,m_end,m_stat,m_product) select '10-641CH',to_date('2008-11-21', 'YYYY-MM-DD'),to_date('2011-07 +-31', 'YYYY-MM-DD'),'Active','694275O' from dual where exists (select NULL from servers where host_serial='10-641CH') ') thr#999c008 dbd_st_prepare'd sql INSERT (pl1, auto_lob1, check_sql1) dbd_describe skipped for INSERT <- prepare= DBI::st=HASH(0x9c5a8dc) at ./get_swma.pl line 57 -> execute for DBD::Oracle::st (DBI::st=HASH(0x9c5a8dc)~0x9c558a8) + thr#999c008 dbd_st_execute INSERT (out0, lob0)... Statement Execute Mode is 32 (COMMIT_ON_SUCCESS) dbd_st_execute INSERT returned (SUCCESS, rpc1, fn3, out0) <- execute= 1 at ./get_swma.pl line 59 -> STORE for DBD::Oracle::st (DBI::st=HASH(0x9c558a8)~INNER 'Trace +Level' 4) thr#999c008 STORE DBI::st=HASH(0x9c558a8) 'TraceLevel' => 4 <- STORE= 1 at ./get_swma.pl line 60 -> DESTROY for DBD::Oracle::st (DBI::st=HASH(0x9c558a8)~INNER) thr +#999c008 <- DESTROY= undef at ./get_swma.pl line 38 via at ./get_swma.pl l +ine 38

        This trace suggests the execute worked as it returned 1. Whether a row was inserted or not is not discernible from this trace since you'd need to know what rows returned.

Re: Oracle Insert with DBI
by mje (Curate) on Jan 12, 2010 at 16:50 UTC

    In your first example what exactly are you getting back from the do method - it should return the number of rows affected, -1 (not known) or undef (error). Which are you getting? Similarly in the second method, what is $stmt->rows after execute?

    UPDATE: bare in mind do returns 0E0 for 0 rows affected which is TRUE but 0.
      for $stmt->rows I get 1s on some and 0s on others. when I use the do .. I get .. 1s on some and OEOs on others

        Well if you get 1 then 1 row should be updated or inserted and if you get 0E0 then no row was updated/inserted. I don't think your SQL is in error if no rows are inserted so why would you think you should get an error if no rows are inserted. If you are saying that when you see 1 in rows or from do, you do not see the update/insert in the database then I'd check you really have AutoCommit on and have not disabled it by calling begin_work.