Oracle:LONG RAW to BLOB - ORA-00932:数据类型不一致



我正在尝试将 LONG RAW 值转换为 BLOB 值,但出现错误:ORA-00932:数据类型不一致:预期的二进制,得到长二进制。代码示例(文档是 LONG RAW 列(:

DECLARE 
  var_blob BLOB;
BEGIN
  select To_Blob(document) into var_blob
    from instructions
    where id = 'XXX';
END;

如果我尝试将代码作为简单的SQL查询(没有PL/SQL代码(执行,则会出现相同的错误。我做错了什么?

编辑:

使用我尝试做的答案中的信息:

create table temp_blob(id VARCHAR2(50), file_contents blob);

然后:

DECLARE
  z_id varchar(50) := 'XXX';
  z_blob blob;
BEGIN
 execute immediate '
  insert into temp_blob
  select :z_id, To_Blob(document)
   from instructions
   where id = :z_id' using z_id, z_id;
 begin
   select file_contents into z_blob
   from temp_blob where id = z_id;
 end;
END;

我仍然遇到同样的错误。还有一些附加信息 - 查询结果的大小:

select document
from instructions
where id = 'XXX';

大于 32760 字节,所以我无法将其分配给 PL/SQL 变量。

你可以从Tom Kyte的博客中获得答案。他已经在这里回答了

编辑:

我们应该使用TO_LOB而不是TO_BLOB。

让我们来看看:

  • 创建两个表:

    create table instructions (id varchar2(50), document long raw);
    create table temp_blob(id VARCHAR2(50), file_contents blob);
    
  • 在 DOCUMENT 列中使用超过 32767 字节的数据填充 INSTRUCTIONS(以确保不会发生隐式转换为 pl/sql varchar2(。为此,我使用这个小解决方案(在 C# 中(:

    using System;
    using System.Data;
    using Oracle.DataAccess.Client;
    namespace LongRaw
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (OracleConnection connection = new OracleConnection("Data Source=XE;User ID=<user_name>;Password=<user_pass>;Pooling=yes;"))
                {
                    connection.Open();
                    using (OracleCommand command = new OracleCommand("insert into instructions values (:id, :document)", connection))
                    {
                        Random random = new Random();
                        byte[] document = new byte[65535];
                        random.NextBytes(document);
                        command.Parameters.Add("id", "XXX");
                        command.Parameters.Add("document", OracleDbType.LongRaw, 65535, val: document, dir: ParameterDirection.Input);
                        command.ExecuteNonQuery();
                    }
                    connection.Close();
                }
            }
        }
    }
    
  • 现在我们在指令中有数据并执行我们的 pl/sql 块:

    SQL> set echo on
    SQL> set serveroutput on
    SQL> 
    declare
        z_id varchar(50) := 'XXX';
        z_blob blob;
    begin
        execute immediate
        'insert into temp_blob
         select :z_id, to_lob(document)
         from   instructions
         where  id = :z_id' using z_id, z_id;
         select file_contents into z_blob
         from temp_blob where id = z_id;
         dbms_output.put_line(dbms_lob.getlength(z_blob) || ' bytes');
    end;
    /
    65535 bytes
    PL/SQL procedure successfully completed
    SQL>
    
  • 我们在这里 - 我们的z_blob包含 65535 字节:)

我很难破解,但得到了一个有效的代码。

declare
l_long_row long raw := rpad( 'a', 2000, 'a' );
l_blob blob;
l_id number;
begin
execute immediate 'select
1,
TO_BLOB(:l_long_row)
into 
:l_id,
:l_blob
from dual'
into l_id, l_blob
using l_long_row;
insert into test_blob
(select
1,
l_blob
from dual);
end;

其中表 test_blob 具有属性 ID(number( 和 BIGBLOB(BLOB(;

希望对您有所帮助。

相关内容

最新更新