In Oracle, writes a string of type VARCHAR2 to a field of BLOB type

Source: Internet
Author: User
Tags commit insert
oracle| string

1, in the database to build a new table for testing.
CREATE Tbale Tb_test
(
ID number,
BLB BLOB
);
COMMIT;

2. Insert a new record into the Tb_test table for testing.
INSERT into Tb_test VALUES (1, Empty_blob ());
COMMIT;
NOTE: Insert a new record into a data table with a BLOB type field, and you cannot fill in the value directly, you must first insert a empty_blob () into the Blob field, and then use Dbms_lob. The Write function writes the value of a blob.

3, write the following string to the BLB field of the record with ID 1: ' Follow I-75 across the Mackinac Bridge. Hello! ';
Declare
Directions BLOB;

Amount Binary_integer;
Offset INTEGER;
First_direction VARCHAR2 (100);
More_directions VARCHAR2 (500);
Begin
Update tb_test Set blb = Empty_blob () where id = 1; --Update and new to set the BLOB field to Empty_blob ()
Select BLB into directions from tb_test where id = 1 for update; --Be sure to lock the record with the for update otherwise
--dbms_lob. Open will be wrong

Dbms_lob. OPEN (directions, Dbms_lob. Lob_readwrite);

First_direction: = ' Follow I-75 across the Mackinac Bridge. Hello! ';
Amount: = LENGTHB (first_direction); --number of characters to write
--Chinese must use LENGTHB
Offset: = 1; --begin writing to the The CLOB
Dbms_lob. WRITE (directions, amount, offset, Utl_raw.cast_to_raw (first_direction));
The--utl_raw.cast_to_raw function converts a string into a binary number

Dbms_lob. Close (directions);
Commit
End



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.