Oracle database: How to read a BLOB?

后端 未结 6 700
星月不相逢
星月不相逢 2021-02-07 23:37

I\'m working with an Oracle database, and I would like to read the contents of a BLOB. How do I do this?

When I do a simple select statement, it merely returns \"(BLOB)

6条回答
  •  名媛妹妹
    2021-02-08 00:02

    You can dump the value in hex using UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2()).

    SELECT b FROM foo;
    -- (BLOB)
    
    SELECT UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2(b))
    FROM foo;
    -- 1F8B080087CDC1520003F348CDC9C9D75128CF2FCA49D1E30200D7BBCDFC0E000000
    

    This is handy because you this is the same format used for inserting into BLOB columns:

    CREATE GLOBAL TEMPORARY TABLE foo (
        b BLOB);
    INSERT INTO foo VALUES ('1f8b080087cdc1520003f348cdc9c9d75128cf2fca49d1e30200d7bbcdfc0e000000');
    
    DESC foo;
    -- Name Null Type 
    -- ---- ---- ---- 
    -- B        BLOB 
    

    However, at a certain point (2000 bytes?) the corresponding hex string exceeds Oracle’s maximum string length. If you need to handle that case, you’ll have to combine How do I get textual contents from BLOB in Oracle SQL with the documentation for DMBS_LOB.SUBSTR for a more complicated approach that will allow you to see substrings of the BLOB.

提交回复
热议问题