GBase 8s clob数据类型操作函数
GBase 8s中clob数据类型的操作一般使用filetoclob,lotofile和locopy函数;
示例如下:
-- 插入文本文件到指定字段中
insert into tabclob(id, clobcol) values (1, filetoclob('/home/gbase/clob.file','client'));
-- 导出clob内容至文件
select lotofile(clobcol,'/home/gbase/clob.file.20220103','client') from tabclob;
-- 复制clob
insert into tabclob2 select 2,locopy(clobcol) from tabclob where id = 1;
现在我们可以使用扩展的函数更便捷的操作clob
包含的内置bld函数如下,包含在excompat.bld中:
dbms_lob_getlength
dbms_lob_substr
dbms_lob_new_clob
我们可以依据此创建clob操作函数;
-- 获取clob的长度
drop function if exists clob_length (clob);
create function clob_length (clob)
returns integer
external name '$GBASEDBTDIR/extend/excompat.1.0/excompat.bld(dbms_lob_getlength)'
language c;
-- clob转换成字符,仅支持32K
drop function if exists clob_to_char(clob,integer,integer);
create function clob_to_char (clob,integer default 32767,integer default 1)
returns lvarchar
external name '$GBASEDBTDIR/extend/excompat.1.0/excompat.bld(dbms_lob_substr)'
language c;
-- char转换为clob
drop function if exists to_clob(lvarchar);
create function to_clob (lvarchar)
returns clob
external name '$GBASEDBTDIR/extend/excompat.1.0/excompat.bld(dbms_lob_new_clob)' language c;
使用示例:
-- 获取clob字段的内容长度
select clob_length(col2) from tabclob;
-- clob转换为char,特别注意第二个参数必须,否则可以越界
select clob_to_char(col2,clob_length(col2)) from tabclob;
-- 字符串转clob
insert into tabclob(col1,col2) values (3,to_clob('to_clob insert'));