Question

I have a Perl script in which I'm trying to use Oracle MERGE statement to upsert data.

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBD::Oracle qw/:ora_types/;
use Data::Dumper;

my $dbh = DBI->connect('db.conn.string', 'user', 'pass', {RaiseError => 1});
$dbh->{LongReadLen} = 16384;

sub main {
        print "started...\n";
        print Dumper { before => select_data() };
        upsert_customer_file_data(112233, 1001, 'sample data here');
        print Dumper { after => select_data() };
        return;
}

sub select_data {
        return $dbh->selectall_arrayref('select * from customer_files');
}

sub upsert_customer_file_data {
        my ($customer_id, $customer_filename_id, $file_data) = @_;
        my $sth = $dbh->prepare(q/
                MERGE INTO customer_files
                USING DUAL ON ( customer_id = :customer_id AND customer_filename_id = :customer_filename_id )
                WHEN MATCHED THEN
                        UPDATE SET customer_file_data = :file_data,
                                   last_modified_date = SYSDATE
                WHEN NOT MATCHED THEN
                        INSERT ( customer_file_id, customer_id, customer_filename_id, customer_file_data, creation_date, last_modified_date )
                        VALUES ( customer_files_seq.nextval, :customer_id, :customer_filename_id, :file_data, SYSDATE, SYSDATE)
        /);
        $sth->bind_param(':customer_id', $customer_id);
        $sth->bind_param(':customer_filename_id', $customer_filename_id);
        $sth->bind_param(':file_data', $file_data, {ora_type => ORA_BLOB});
        $sth->execute();
        return $sth->rows();
}

main();

When I run the above script I get the following error when it gets to upsert_customer_file_data(112233, 1001, 'sample data here'); :

DBD::Oracle::st execute failed: ORA-01403: no data found (DBD ERROR: LOB refetch attempted for unsupported statement type (see also ora_auto_lob attribute)) 

When I run this SQL in PLSQL Developer there is no problem, but DBI does not like it and I cannot figure out why.

Appreciate any help.

Thanks,
Alex

Was it helpful?

Solution 2

Ok, I figured out what the problem was - my ora_type is wrong (in binding: {ora_type => ORA_BLOB}). I changed it to SQLT_BIN and it works perfectly fine. What makes it really confusing is that {ora_type => ORA_BLOB} works fine with regular UPDATE or INSERT but not with MERGE

OTHER TIPS

have you tried ora_pers_lob flag ?

   my $sth = $dbh->prepare(q/
            MERGE INTO customer_files
            USING DUAL ON ( customer_id = :customer_id AND customer_filename_id =   :customer_filename_id )
            WHEN MATCHED THEN
                    UPDATE SET customer_file_data = :file_data,
                               last_modified_date = SYSDATE
            WHEN NOT MATCHED THEN
                    INSERT ( customer_file_id, customer_id, customer_filename_id, customer_file_data, creation_date, last_modified_date )
                    VALUES ( customer_files_seq.nextval, :customer_id, :customer_filename_id, :file_data, SYSDATE, SYSDATE)
    /, ,{ora_pers_lob => 1});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top