尝试使用Perl DBI中的Oracle MERGE进行upsert时接收ORA-01403

时间:2013-12-22 21:05:22

标签: sql oracle perl blob dbi

我有一个Perl脚本,我正在尝试使用Oracle MERGE语句来插入数据。

#!/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();

当我运行上面的脚本时,当它到达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)) 

当我在PLSQL Developer中运行此SQL时没有问题,但DBI不喜欢它,我无法弄清楚原因。

感谢任何帮助。

谢谢,
亚历

2 个答案:

答案 0 :(得分:2)

你试过ora_pers_lob标志吗?

   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});

答案 1 :(得分:1)

好的,我弄清楚问题是什么 - 我的ora_type错了(绑定:{ora_type => ORA_BLOB})。我将其更改为SQLT_BIN,它完全正常。真正令人困惑的是{ora_type => ORA_BLOB}适用于常规UPDATEINSERT,但不适用于MERGE