Oracle中,将VARchar2类型的字符串写入BLOB类型的字段中

1、在数据库中建一个新表用于测试。
create tbale tb_test
(
    id number,
    blb blob
);
commit;

2、往tb_test表中插入一条新记录用于测试。
insert into tb_test values(1, empty_blob());
commit;
注:往有blob类型的字段的数据表中插入新记录,不能直接填入值,必须先往blob字段插入一个empty_blob(),然后再用dbms_lob.write函数写入blob的值。

3、向id为1的记录的blb字段写入以下字符串:'follow i-75 across the mackinac bridge.你好!';
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;      --更新和新增一样要将blob字段设置为empty_blob()
    select blb into directions from tb_test where id = 1 for update; --一定要用for update锁住记录,否则        
                                                                                                                   --dbms_lob.open会出错

    dbms_lob.open(directions, dbms_lob.lob_readwrite);

    first_direction := 'follow i-75 across the mackinac bridge.你好!';
    amount := lengthb(first_direction);  --number of characters to write
                                                                         --有中文必须用lengthb
       offset := 1; --begin writing to the first character of the clob
        dbms_lob.write(directions, amount, offset, utl_raw.cast_to_raw(first_direction));
        --utl_raw.cast_to_raw函数将字符串转换成二进制数

    dbms_lob.close(directions);
    commit;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值