GBase 8s clob数据类型操作函数
GBase 8s clob数据类型操作函数
GBase 8s中clob数据类型的操作一般使用filetoclob,lotofile和locopy函数;
示例如下:
1 2 3 4 5 6 | -- 插入文本文件到指定字段中 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中:
1 2 3 | dbms_lob_getlength dbms_lob_substr dbms_lob_new_clob |
我们可以依据此创建clob操作函数;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | -- 获取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; |
使用示例:
1 2 3 4 5 6 | -- 获取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' )); |
20230301增加函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | -- 可增加自定义函数,对clob的输出长度进行限制。 -- 输出整个clob,大于32767仅显示32767 drop function if exists clob_to_char(clob); create function clob_to_char(pclob clob) returns lvarchar return clob_to_char(pclob, LEAST(clob_length(pclob),32767)); end function ; -- 输出指定长度的clob,大于32767仅显示32767 drop function if exists clob_to_char(clob, integer ); create function clob_to_char(pclob clob,plen integer ) returns lvarchar return clob_to_char(pclob, LEAST(clob_length(pclob),plen,32767),1); end function ; |
- 上一篇: GBase 8s 自定义mysql兼容函数
- 下一篇: GBase 8s数据库JDBC连接字符串