oracle 中向blob中写入varchar2型数据

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; 

 

//---------------------------------------------------------------------------------------------------------------------------------

上面是在网上看到的,下面是我试的另一种方法:

create or replace procedure InsertNewRecord
is
filename blob;

buffer varchar2(100);
amount INT;
begin
buffer := '我的祖国,伟大的祖国';
amount :=LENGTHB(buffer);


insert into p_fax_send (fax_send_id,file_name) values(1001,EMPTY_BLOB());

select file_name into filename from p_fax_send where fax_send_id = 1001 for update;


dbms_lob.writeappend(filename,amount,UTL_RAW.cast_to_raw(buffer));
commit;

end InsertNewRecord;

 

不用UTL_RAW.cast_to_raw()的话会报错,类型不匹配。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值