简体   繁体   English

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

[英]Receiving ORA-01403 when trying to upsert using Oracle MERGE in Perl DBI

I have a Perl script in which I'm trying to use Oracle MERGE statement to upsert data. 我有一个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();

When I run the above script I get the following error when it gets to upsert_customer_file_data(112233, 1001, 'sample data here'); 当我运行上面的脚本时,我得到以下错误,当它到达upsert_customer_file_data(112233,1001,'这里的样本数据'); :

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. 当我在PLSQL Developer中运行这个SQL时没有问题,但DBI不喜欢它,我无法弄清楚原因。

Appreciate any help. 感谢任何帮助。

Thanks, 谢谢,
Alex 亚历克斯

have you tried ora_pers_lob flag ? 你试过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});

Ok, I figured out what the problem was - my ora_type is wrong (in binding: {ora_type => ORA_BLOB} ). 好吧,我弄清楚问题是什么 - 我的ora_type错了(在绑定中: {ora_type => ORA_BLOB} )。 I changed it to SQLT_BIN and it works perfectly fine. 我把它改成了SQLT_BIN ,它运行得很好。 What makes it really confusing is that {ora_type => ORA_BLOB} works fine with regular UPDATE or INSERT but not with MERGE 真正令人困惑的是{ora_type => ORA_BLOB}在常规UPDATEINSERT工作正常但与MERGE不兼容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM