开发者

How can I append/concatenate BLOB data to a BLOB column using SQL UPDATE command in ORACLE

开发者 https://www.devze.com 2023-03-16 23:16 出处:网络
I need to append data to my BLOB field, how can I do this using an UPDATE command? What i am asking is; is it possible to concatenate blob data so that i can eventually set it to a field like

I need to append data to my BLOB field, how can I do this using an UPDATE command? What i am asking is; is it possible to concatenate blob data so that i can eventually set it to a field like UPDATE BLOB_table SET BLOB_field = BLOB_field + BLOB_data

I tried using DBMS_LOB.APPEND but it does not return a value; so i created a function which gives me an error of "invalid LOB locator specified"

CREATE OR REPLACE FUNCTION MAKESS.CONCAT_BLOB(A in BLOB,B in BLOB) RETURN BLOB IS
 C BLOB;
BEGIN
DBMS_LOB开发者_Python百科.APPEND(c,A);
DBMS_LOB.APPEND(c,B);
RETURN c;
END;
/


You need to create a temporary blob with DBMS_LOB.createtemporary:

SQL> CREATE OR REPLACE FUNCTION CONCAT_BLOB(A IN BLOB, B IN BLOB) RETURN BLOB IS
  2     C BLOB;
  3  BEGIN
  4     dbms_lob.createtemporary(c, TRUE);
  5     DBMS_LOB.APPEND(c, A);
  6     DBMS_LOB.APPEND(c, B);
  7     RETURN c;
  8  END;
  9  /

Function created

Then you should be able to use it in an update statement:

SQL> CREATE TABLE t (a BLOB, b BLOB, c BLOB);

Table created

SQL> INSERT INTO t VALUES
  2     (utl_raw.cast_to_raw('aaa'), utl_raw.cast_to_raw('bbb'), NULL);

1 row inserted

SQL> UPDATE t SET c=CONCAT_BLOB(a,b);

1 row updated

SQL> SELECT utl_raw.cast_to_varchar2(a),
  2         utl_raw.cast_to_varchar2(b),
  3         utl_raw.cast_to_varchar2(c)
  4  FROM t;

UTL_RAW.CAST_TO_VARCHAR2(A UTL_RAW.CAST_TO_VARCHAR2(B UTL_RAW.CAST_TO_VARCHAR2(C
-------------------------- -------------------------- --------------------------
aaa                        bbb                        aaabbb 


With help of PL/SQL blob can be updated in place with no need for custom function at all:

BEGIN
   FOR c IN (select a, b from t where a is not null for update) LOOP
       DBMS_LOB.APPEND(c.a, c.b);
   END LOOP;
END;
/
0

精彩评论

暂无评论...
验证码 换一张
取 消