SAP中连接外部Oracle数据库的字符集转换

在non-Unicode的SAP系统中连接外部AL32UTF8的Oracle数据库,对于中文字符,以前一直认为需要程序中做编码转换。
今天根据note 808505的说明,在连接字符串后增加编码说明(TNS_NAME:ZHS16GBK)后,中文字符由Oracle自动转换编码后,可以直接在ABAP中像英文字符一样正常使用。

以前的处理方法挺麻烦的,大概过程如下:
读取:在Oracle中先转换为16进制的RAW类型,然后在sap中进行编码转换,代码如下:

select utl_raw.cast_to_raw('测试') from dual;
form hex_to_utf8 using p_hex_source type xsequence
changing p_result type simple.
data: cv type ref to cl_abap_conv_in_ce.

cv = cl_abap_conv_in_ce=>create( encoding = 'UTF-8' ).

call method cv->convert
exporting
input = p_hex_source
importing
data  = p_result.
endform.

写入:在SAP中先转换为16进制,然后在Oracle中转换为实际的数据类型,代码如下:

form utf8_to_hex using p_source type simple
changing p_hex_result type xsequence.
data: cv type ref to cl_abap_conv_out_ce.

cv = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
call method cv->convert
exporting
data   = p_source
importing
buffer = p_hex_result.
endform.
insert into t values (utl_raw.cast_to_varchar2(:name))

Comments are closed.