How to concatenate BLOB fields (Oracle)?

痴心易碎 提交于 2020-07-09 14:00:14

问题


IS it possible to concatenate A1 and A2 from the particular table (for example):

CREATE TABLE MY_SCHEME.CONC_BLOB
(
  A1       BLOB,
  A1_SORT  NUMBER(20),
  T_TYPE   VARCHAR2(9 BYTE),
  A2       BLOB,
  A2_SORT  NUMBER(20),
  A3       VARCHAR2(32 BYTE),
  A4       BLOB,
  A5       VARCHAR2(8 BYTE)
)

? How?


回答1:


BLOBs can be concatenated with the DBMS_LOB package, in particular with the APPEND procedure. But you will need to use some PL/SQL that iterates over the relevant rows and calls the procedure.

I don't quite understand what you mean by next table so I can't give you an example.

Update:

The relevant piece of PL/SQL could look like this:

DECLARE
  a1_lob BLOB;
  a2_lob  BLOB;

BEGIN
  SELECT A1, A2 INTO a1_lob, a2_lob
  FROM CONC_BLOB
  WHERE A1_SORT = 'some value'
  FOR UPDATE;

  dbms_lob.append(a1_lob, a2_lob);
  COMMIT;
END;



回答2:


FYI: if you intent to use blob to store large text (that's why I suppose you would like to concatenate them) I suggest using CLOB. It will permit you to use || for the best part of concatenations. Unfortunately you could face with the issue of || when the length of clob exceeds 32767




回答3:


Here is my solution for joining any number of BLOBs into single BLOB using helper table type and stored function:

create or replace type blobs as table of blob;

create or replace function concat_blobs(parts in blobs) return blob
is
    temp blob;
begin
    if parts is null or parts.count = 0 then
       return null;
    end if;
    dbms_lob.createtemporary(temp, false, dbms_lob.CALL);
    for i in parts.first .. parts.last
    loop
        dbms_lob.append(temp, parts(i));
    end loop;
    return temp;
end;

-- usage example:
select concat_blobs(blobs(to_blob(hextoraw('CAFE')), to_blob(hextoraw('BABE')))) from dual;


-- bonus
create or replace type raws as table of raw(2000);

create or replace function raws_to_blobs(arg in raws) return blobs
is
    res blobs;
begin
    select to_blob(column_value) bulk collect into res from table(arg);
    return res;
end;

-- usage example:
select concat_blobs(raws_to_blobs(raws(hextoraw('CAFE'), hextoraw('BABE'))) from dual;

See also multiple RAWs concatenation in Oracle 10: Using HEXTORAW to fill in blob data.



来源:https://stackoverflow.com/questions/12038066/how-to-concatenate-blob-fields-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!