XSTRING的介绍
SAP ABAP 理解RAWSTRING(XSTRING) 类型-腾讯云开发者社区-腾讯云
XString,String以及SString 类型区别 | 摆渡SAP
SAP ABAP 理解RAWSTRING(XSTRING) 类型
RAWSTRING 和 STRING 类型具有可变长度。可以指定这些类型的最大长度,但没有上限。
SSTRING 类型从版本 6.10 开始可用,并且具有可变长度。必须指定其最大长度,并且限制为 255。与 CHAR 相比,此类型的优点是它被分配给 ABAP 类型字符串。
这里不对
SSTRING 是release 6.10之后才有的一个数据类型,长度也可变,但是最长只能指定255个字符。长度是可变的,其最大值必须指定且上限为255。与CHAR类型相比其优势是它与ABAP type string进行映射。
https://www.cnblogs.com/jiangzhengjun/p/4292791.html
string类型 = sstring类型.可以直接赋值"不会出错.
1)xstring不支持中文,
DATA str TYPE sstring.
DATA str1 TYPE sstring.
DATA str2 TYPE xstring. "这个就是rawstring类型
DATA str3 TYPE string.
str1 = '天天向上'.
str2 = '天天向上'.
str3 = '天天向上'.
WRITE :/ 'sstring:',str1.
WRITE :/ 'xstring:',str2.
WRITE :/ 'string:',str3.
2)xstring支持6个字母
DATA str TYPE sstring.
DATA str1 TYPE sstring.
DATA str2 TYPE xstring. "这个就是rawstring类型
DATA str3 TYPE string.
str1 = 'ABCDEFGH'.
str2 = 'ABCDEFGH'.
str3 = 'ABCDEFGH'.
WRITE :/ 'sstring:',str1.
WRITE :/ 'xstring:',str2.
WRITE :/ 'string:',str3.
3)Xstring不支持小写字母
DATA str TYPE sstring.
DATA str1 TYPE sstring.
DATA str2 TYPE xstring. "这个就是rawstring类型
DATA str3 TYPE string.
str1 = 'abcdefgh'.
str2 = 'abcdefgh'.
str3 = 'abcdefgh'.
WRITE :/ 'sstring:',str1.
WRITE :/ 'xstring:',str2.
WRITE :/ 'string:',str3.
4)当xstring一旦遇到不支持的,立刻停止,还有,大家要注意后面的0
DATA str TYPE sstring.
DATA str1 TYPE sstring.
DATA str2 TYPE xstring. "这个就是rawstring类型
DATA str3 TYPE string.
str1 = 'ABCefgh'.
str2 = 'ABCefgh'.
str3 = 'ABCefgh'.
WRITE :/ 'sstring:',str1.
WRITE :/ 'xstring:',str2.
WRITE :/ 'string:',str3.
5)如果xstring的变量,没有到6个字母的时候,都会后面补0
DATA str TYPE sstring.
DATA str1 TYPE sstring.
DATA str2 TYPE xstring. "这个就是rawstring类型
DATA str3 TYPE string.
str1 = 'ABC'.
str2 = 'ABC'.
str3 = 'ABC'.
WRITE :/ 'sstring:',str1.
WRITE :/ 'xstring:',str2.
WRITE :/ 'string:',str3.
当一个xstring变量不支持的值,赋值给它的时候,它会取到自己能读取的部分,而且就算最后想还原,也不可能了。
STRING类型转xstring类型
SAP-ABAP-740新语法解析中文版三:转换操作符CONV_abap conv_Qunending的博客-CSDN博客
方法一:
740前
我们可以使用 方法 cl_abap_codepage=>convert_to 来转换字段类型,如下列代码
DATA text TYPE c LENGTH 255.DATA helper TYPE string.DATA xstr TYPE xstring.helper = text. "char类型转换为string类型直接用等于赋值即可 helper = text.
xstr = cl_abap_codepage=>convert_to( source = helper ). "string类型转换为xstring类型
740之后
可以结合快速定义来转换类型
DATA text TYPE c LENGTH 255.DATA(xstr1) = cl_abap_codepage=>convert_to( source = CONV string( text ) ).""指定转换为xstring类型DATA(xstr2) = cl_abap_codepage=>convert_to( source = CONV #( text ) ).""根据上下文代码隐式转换
上面的代码运行结束后xstr1,xstr2都是xstring类型
Before 7.40 |
---|
DATA text TYPE c LENGTH 255. DATA helper TYPE string. DATA xstr TYPE xstring. helper = text. xstr = cl_abap_codepage=>convert_to( source = helper ). |
With 7.40 |
DATA text TYPE c LENGTH 255. DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV string( text ) ). OR DATA(xstr) = cl_abap_codepage=>convert_to( source = CONV #( text ) ). |
方法二:
SCMS_STRING_TO_XSTRING
*string转xstring.
data:xstr type xstring.
data:str type string value 'ABC'.
data:length type i.
length = strlen( str ).
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'"string转xstring函数EXPORTINGtext = str"可以是string或char类型的,
* MIMETYPE = ' '
* ENCODING =IMPORTINGBUFFER = xstrEXCEPTIONSFAILED = 1OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.write:/ 'XSTRING类型输出:'.
uline.
write:/ xstr."结果414243
方法三:
DATA: str TYPE string ,xstr TYPE xstring.
*TRY.
CALL METHOD cl_bcs_convert=>string_to_xstringEXPORTINGiv_string = 'ABC'
* iv_convert_cp = 'X'
* iv_codepage =
* iv_add_bom =receivingev_xstring = xstr..
WRITE / : xstr."结果414243
* CATCH cx_bcs .
*ENDTRY.
将xstring转为string
** solution 1CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'EXPORTINGfrom_codepage = lv_encodingin_xstring = lv_upload_fileIMPORTINGout_string = lv_out_string.
** solution 2
* CALL FUNCTION 'LXE_COMMON_XSTRING_TO_STRING'
* EXPORTING
* IN_CODEPAGE = lv_codepage
* in_xstring = lv_upload_file
* IMPORTING
* EX_STRING = lv_out_string.
** solution 3
* DATA: lv_filesize TYPE i,
* lt_bin_data TYPE STANDARD TABLE OF raw255."x类型二进制表即可
*
* CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
* EXPORTING
* buffer = lv_upload_file
* IMPORTING
* output_length = lv_filesize
* TABLES
* binary_tab = lt_bin_data.
*
* CALL FUNCTION 'SCMS_BINARY_TO_STRING'
* EXPORTING
* input_length = lv_filesize
** MIMETYPE = ' '
* ENCODING = lv_encoding
* IMPORTING
* text_buffer = lv_out_string
** OUTPUT_LENGTH =
* TABLES
* binary_tab = lt_bin_data
* EXCEPTIONS
* failed = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
** Implement suitable error handling here
* ENDIF.
** solution 4
*lv_out_string = CL_BCS_CONVERT=>XSTRING_TO_STRING(
* IV_XSTR = lv_upload_file
* IV_CP = lv_codepage ).
** solution 5
* CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
* EXPORTING
* im_xstring = lv_upload_file
* IM_ENCODING = lv_encoding
* IMPORTING
* EX_STRING = lv_out_string .
方法一:
ECATT_CONV_XSTRING_TO_STRING
data:xml_xstring type xstring,xml_string type string.CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'EXPORTINGim_xstring = xml_xstring "二进制字符串im_encoding = 'UTF-8'IMPORTINGex_string = xml_string. "返回的xml字符串
方法二:
data: xmlstr type string,character_set type string value 'UTF-8',xmlxstr type xstring value '414243'.xmlstr = cl_abap_codepage=>convert_from( source = xmlxstr codepage = character_set ).WRITE: '字符串:', xmlstr.
结果:
方法三:可以防止乱码
HR_KR_XSTRING_TO_STRING
DATA: lv_codepage_numc TYPE cpcodepage,lv_upload_file type xstring value '414241D6D0B9FA',lv_out_string type string,lv_encoding type ABAP_ENCODING.CALL FUNCTION 'NLS_GET_FRONTEND_CP'EXPORTINGlangu = sy-langufetype = 'MS'IMPORTINGfrontend_codepage = lv_codepage_numcEXCEPTIONSillegal_syst_codepage = 1no_frontend_cp_found = 2internal_or_db_error = 3OTHERS = 4.IF sy-subrc <> 0.RETURN.ENDIF.
"其中from_codepage为Frontend codepage,可使用上面的方法进行获取:lv_encoding = lv_codepage_numc."8404
CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'EXPORTINGfrom_codepage = lv_encodingin_xstring = lv_upload_fileIMPORTINGout_string = lv_out_string.write lv_out_string."结果ABA中国
方法四:
Xstring to String
SCMS_XSTRING_TO_BINARY ->SCMS_BINARY_TO_STRING
DATA: l_cntbin TYPE sdokcntbins,length TYPE I,XSTR TYPE XSTRING VALUE '41424344',str4 TYPE STRING.
REFRESH l_cntbin.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTINGBUFFER = XSTR" APPEND_TO_TABLE = ' '
IMPORTINGOUTPUT_LENGTH = length
TABLESbinary_tab = l_cntbin.
IF sy-subrc = 0.* 二进制内表转stringCALL FUNCTION 'SCMS_BINARY_TO_STRING'EXPORTINGinput_length = length
* FIRST_LINE = 0
* LAST_LINE = 0
* MIMETYPE = ' '
* ENCODING =IMPORTINGTEXT_BUFFER = str4OUTPUT_LENGTH = lengthTABLESbinary_tab = l_cntbinEXCEPTIONSFAILED = 1OTHERS = 2.IF sy-subrc <> 0.
* Implement suitable error handling hereENDIF.WRITE: '字符串:', STR4."结果ABCD
ENDIF.
方法五:
LXE_COMMON_XSTRING_TO_STRING
DATA:in_xstring TYPE XSTRING VALUE '414243',EX_STRING TYPE STRING.CALL FUNCTION 'LXE_COMMON_XSTRING_TO_STRING'EXPORTINGin_xstring = in_xstring
* IN_CODEPAGE = '4110'
* EX_CODEPAGE = '0000'
* UNMASK_CRLF = ''IMPORTINGEX_STRING = EX_STRINGEXCEPTIONSERROR = 1OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
WRITE EX_STRING ."结果ABC
方法六:
CL_BCS_CONVERT
DATA: str TYPE string ,xstr TYPE xstring. DATA:character_set type string value 'UTF-8'.
CALL METHOD cl_bcs_convert=>xstring_to_stringEXPORTINGiv_xstr = '414241D6D0B9FA'"'4261727279'iv_cp = '8404'"'UTF-8'RECEIVINGrv_string = str.
WRITE / : str."结果ABA中国
Binary to Xstring(将二进制内表转换XSTRING)
SCMS_BINARY_TO_XSTRING
DATA: l_cntbin2 TYPE sdokcntbins,length1 TYPE I,XSTRING TYPE XSTRING.
DATA:LS_CNTBIN LIKE LINE OF l_cntbin2.
length1 = 3 .LS_CNTBIN-LINE = '414243'."XSTR.
APPEND LS_CNTBIN TO l_cntbin2.
CLEAR LS_CNTBIN.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'EXPORTINGinput_length = length1 "需要二位算一个长度
* FIRST_LINE = 0
* LAST_LINE = 0IMPORTINGBUFFER = XSTRINGtablesbinary_tab = l_cntbin2
* EXCEPTIONS
* FAILED = 1
* OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
WRITE: 'XSTRING字符串:', XSTRING."结果414243
常与xml上传连用
TYPES: BEGIN OF xml_line,DATA(512) TYPE X,"这里的长度设置不会影响输出结果,设置成1都可以
END OF xml_line.
DATA: l_xml_table TYPE TABLE OF xml_line WITH HEADER LINE,l_xml_size TYPE I.
DATA xmldata TYPE xstring .
"如果需要上载XML可以用一下方法
CALL FUNCTION 'GUI_UPLOAD'
EXPORTINGfilename = 'C:\temp\test.xml'filetype = 'BIN'
IMPORTINGfilelength = l_xml_size
TABLESdata_tab = l_xml_table."x类型表
"将二进制内表转换(拼接)成一个二进制串
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTINGinput_length = l_xml_size "需要有值
IMPORTINGBUFFER = xmldata
TABLESbinary_tab = l_xml_table[].
Xstring to Binary(将XSTRING转换二进制内表)
SCMS_XSTRING_TO_BINARY
*TYPES: BEGIN OF xml_line,
* DATA(512) TYPE X,"这里的长度设置不会影响输出结果,设置成1都可以
*END OF xml_line.
*DATA: l_cntbin2 TYPE TABLE OF xml_line WITH HEADER LINE.
DATA: l_cntbin2 TYPE sdokcntbins,"只要是x类型的内表即可length TYPE I,XSTRING TYPE XSTRING value '414243'.
DATA:LS_CNTBIN LIKE LINE OF l_cntbin2.CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'EXPORTINGbuffer = XSTRING "''" APPEND_TO_TABLE = ' 'IMPORTINGOUTPUT_LENGTH = lengthtablesbinary_tab = l_cntbin2.
LOOP AT l_cntbin2 INTO LS_CNTBIN.
WRITE: 'XSTRING字符串:','长度:',length, LS_CNTBIN-LINE."结果3 41424300000000000000
ENDLOOP.
Base64 Encoding
SCMS_STRING_TO_XSTRING.
再使用 SCMS_BASE64_ENCODE_STR 函数将 Xstring 转换为 Base64。
DATA: instr TYPE string,xstr TYPE xstring,encode_str TYPE string,mimetype(128) TYPE C.
INSTR = 'DDDD'.
CONCATENATE '"text/html; charset=' 'GBK' '"' INTO mimetype.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'EXPORTINGtext = instrMIMETYPE = mimetype "有这个不会出现中文乱码的问题
* ENCODING =IMPORTINGBUFFER = xstrEXCEPTIONSFAILED = 1OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'EXPORTINGinput = xstrIMPORTINGOUTPUT = encode_str.
Base64 Decoding:
方法一:
使用 SCMS_BASE64_DECODE_STR 将 Base64 转换为 Xstring,然后使用 FM:SCMS_XSTRING_TO_BINARY 将 Xstring 转换为二进制。最后,通过 SCMS_BINARY_TO_STRING 将 二进制 转换为string。
方法二:
DATA: instr TYPE string VALUE 'DDD',"原结果xstr TYPE xstring,charset TYPE string value 'GBK'.
DATA: out TYPE string VALUE 'RERE'.CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
EXPORTINGINPUT = OUT
* unescape = 'X'
IMPORTINGOUTPUT = xstr
EXCEPTIONSfailed = 1OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.WRITE : / 'Decode:' ,xstr.
DATA: codepage(4) TYPE n.
DATA: ENCODING(20) TYPE C.
DATA: convin TYPE REF TO cl_abap_conv_in_ce.
DATA: str TYPE string.CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
EXPORTINGexternal_name = ''"charset这里为空或者为'GBK'都可以还原
IMPORTINGsap_codepage = codepage
EXCEPTIONSnot_found = 1OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENCODING = codepage."创建解码对象
convin = cl_abap_conv_in_ce=>create( ENCODING = ENCODING INPUT = xstr ).
CALL METHOD convin->read( IMPORTING DATA = str ).
WRITE: / 'Encode:' ,str.
科学计数法转换数字
QSS0_FLTP_TO_CHAR_CONVERSION
data:z_value type CHA_CLASS_DATA-SOLLWERT,"科学计数法z_value1 TYPE CHA_CLASS_VIEW-SOLLWERT."正常数但是char类型
DATA: FLA TYPE P DECIMALS 2 VALUE '31400000000.00'.
z_value = FLA.CALL FUNCTION 'QSS0_FLTP_TO_CHAR_CONVERSION'EXPORTINGi_number_of_digits = 0i_fltp_value = z_value
* I_VALUE_NOT_INITIAL_FLAG = 'X'
* I_SCREEN_FIELDLENGTH = 16IMPORTINGE_CHAR_FIELD = z_value1.
WRITE z_value1.
CALL FUNCTION 'QSS0_CHAR_FLTP_CONVERSION'EXPORTINGI_CHAR_FIELD_NAME = ''i_character_string = z_value1i_number_of_digits = ''IMPORTING
* E_VALUE_NOT_INITIAL_FLAG =E_FLTP_VALUE = z_value.
WRITE z_value.
QSS0_CHAR_FLTP_CONVERSION
方法二:
data:z_value type CHA_CLASS_DATA-SOLLWERT."科学计数法
DATA: FLA TYPE P DECIMALS 2 VALUE '31400000000.00'.
z_value = FLA."直接实现科学计数法
ABAP中的长文本
在ABAP中,我们经常会用到长文本来描述详细信息。
长文本存储在表格:STXH(抬头),STXL(行项目)中。
1.STXH:长文本抬头表
2.STXL:长文本行表
3.TTXID:Text ID 表
4.TTXOB:Textobject表
5.订单中众多的文本描述,我们怎么知道其对应的【对象】&【ID】呢?
可SE38-通过查找程式:RSTXTC3
也可以在对应的订单文本中查看,点击明细,用WORD查看
6.【对象】和【ID】的配置可在TCODE:SE75中
长文本中有四个关键属性:
object、id、name、langguge
即:
TDOBJECT
TDNAME
TDID
TDSPRAS
其中language不必解释,object 和 id 可以在se75中进行配置,一个object 可以对一个多个ID,比如我们常用的 TEXT + ST
name 一般用对象号。
而对长文本的操作主要是 READ_TEXT 和 SAVE_TEXT
分别实现长文本的读取和写入,例如:
REPORT ZLJC9999.
DATA: TD_NAME LIKE STXH-TDNAME.
DATA ITAB_LINE LIKE TLINE OCCURS 0 WITH HEADER LINE.
TD_NAME = 'LJC0000001'.
CLEAR ITAB_LINE.
REFRESH ITAB_LINE.ITAB_LINE-TDLINE = 'ARE YOU DOG?'. "把多行长文本加在一起
APPEND ITAB_LINE.DATA:L_THEAD TYPE THEAD.
L_THEAD-TDOBJECT = 'ZLJC'.L_THEAD-TDNAME = TD_NAME.
L_THEAD-TDID = 'SB'.
L_THEAD-TDSPRAS = SY-LANGU.CALL FUNCTION 'SAVE_TEXT'EXPORTINGHEADER = L_THEADTABLESLINES = ITAB_LINE.
COMMIT WORK.CALL FUNCTION 'READ_TEXT' EXPORTINGCLIENT = SY-MANDTID = L_THEAD-TDIDLANGUAGE = SY-LANGUNAME = TD_NAMEOBJECT = L_THEAD-TDOBJECTTABLESLINES = ITAB_LINEEXCEPTIONSNOT_FOUND = 4.IF SY-SUBRC = 0.LOOP AT ITAB_LINE.WRITE: ITAB_LINE-TDLINE . "把多行长文本加在一起ENDLOOP.
ENDIF.
一般我们还会提供长文本的录入、编辑、显示界面。这个需要用面向OO的组件。在屏幕上画一个Container。
初始化长文本界面的例子:
DATA:ppcont_desc1 TYPE REF TO cl_gui_custom_container,ppedit_desc1 TYPE REF TO cl_gui_textedit,create object: ppcont_desc1 exporting container_name = 'LONGTEXT1',ppedit_desc1 exporting parent = ppcont_desc1.call method ppedit_desc1->set_toolbar_modeexportingtoolbar_mode = cl_gui_textedit=>true. "长文本 菜单加载 FALSE 取消菜单call method ppedit_desc1->set_statusbar_modeexportingstatusbar_mode = cl_gui_textedit=>false.call method ppedit_desc1->register_event_dblclickexportingappl_event = 'X'.call method ppedit_desc1->set_visibleexportingvisible = 'X'.长文本写到编辑区域的例子:l_tdid = 'ST'.l_tdobject = 'TEXT'.refresh: it_tab3[],text_tab[].clear it_tab3.call function 'READ_TEXT'exportingid = l_tdidlanguage = sy-languname = p_tdnameobject = l_tdobjecttableslines = it_tab3exceptionsid = 1language = 2name = 3not_found = 4object = 5reference_check = 6wrong_access_to_archive = 7others = 8.if sy-subrc <> 0.endif.loop at it_tab3.line = it_tab3-tdline.append line to text_tab.endloop.refresh it_tab3[].call method ppedit_desc1->set_text_as_r3tableexportingtable = text_tab
* EXCEPTIONS
* error_dp = 1
* error_cntl_call_method = 2
* error_dp_create = 3
* potential_data_loss = 4
* OTHERS = 5.refresh text_tab[].将界面中的长文本保存的例子:break ljc.refresh: text_tab[],it_tab3.
* CALL METHOD ppedit_desc->get_text_as_r3table
* IMPORTING
* table = text_tab.call method ppedit_desc->get_text_as_r3tableexportingonly_when_modified = ''importing
* TEXT = TEXT_TAB.table = text_tabexceptionserror_dp = 1error_cntl_call_method = 2error_dp_create = 3potential_data_loss = 4others = 5.loop at text_tab into field.it_tab3-tdline = field.append it_tab3.clear: field,it_tab3.endloop.
**** 保存长文本l_thead-tdobject = 'TEXT'.clear l_thead-tdname.
* concatenate 'PP' ZPLM090_PP-PPNUM into L_THEAD-TDNAME.l_thead-tdname = tab_check-objnr.l_thead-tdid = 'ST'.l_thead-tdspras = sy-langu.call function 'SAVE_TEXT'exportingheader = l_theadtableslines = it_tab3.commit work.
长文本ID
DATA:lv_str TYPE STRING.
"读取长文本---通过id
CALL METHOD cl_eso_extraction_tools=>extract_long_text_by_idEXPORTINGiv_langu = sy-langu "语言iv_text_id = 'F01' "文本标识iv_name = '4500000151' "文本名iv_object = 'EKKO' "文本对象IMPORTINGev_search_terms = lv_str..WRITE :lv_str.
读取速度提升
TYPES: BEGIN OF ty_stxl_raw, "压缩的文本clustr TYPE stxl-clustr,clustd TYPE stxl-clustd,END OF ty_stxl_raw.TYPES:BEGIN OF ty_text, "解压的文本tdobject TYPE stxl-tdobject,tdid TYPE stxl-tdid,tdname TYPE stxl-tdname,tdline TYPE string,END OF ty_text.TYPES: BEGIN OF ty_stxl, "直接查询stxl的数据格式tdobject TYPE stxl-tdobject,tdid TYPE stxl-tdid,tdname TYPE stxl-tdname,clustr TYPE stxl-clustr,clustd TYPE stxl-clustd,END OF ty_stxl.DATA: lt_stxl TYPE TABLE OF ty_stxl WITH HEADER LINE. "DATA: lt_stxl_raw TYPE TABLE OF ty_stxl_raw WITH HEADER LINE,ls_stxl_raw TYPE ty_stxl_raw.
DATA lt_tline TYPE STANDARD TABLE OF tline WITH HEADER LINE.DATA lt_text TYPE TABLE OF ty_text .
DATA ls_text TYPE ty_text."查询销售订单以及订单行
SELECT vbak~vbeln,vbap~posnr,vbak~ernam,CAST( vbak~ernam AS CHAR( 200 ) ) AS longtexth,"抬头长文本CAST( vbak~ernam AS CHAR( 200 ) ) AS longtext, "定义一个200长度的字段,里面的内容后面直接清空掉concat( vbak~vbeln ,vbap~posnr ) AS tdname "订单 + 订单行 = TDNAMEFROM vbakINNER JOIN vbap ON vbak~vbeln EQ vbap~vbeln
WHERE vbak~vbeln = '0000013856' "指定的销售订单号,作为测试INTO TABLE @DATA(lt_vbak)."查询抬头文本
"先给我销售订单去重,一个销售订单一行
DATA(lt_vbak_h) = lt_vbak[].
SORT lt_vbak_h BY vbeln .
DELETE ADJACENT DUPLICATES FROM lt_vbak_h COMPARING vbeln .SELECT stxl~tdobject,stxl~tdid,stxl~tdname,stxl~clustr,stxl~clustdFROM stxl
INNER JOIN @lt_vbak_h AS l ON l~vbeln EQ stxl~tdname
WHERE stxl~tdobject = 'VBBK'
AND stxl~tdid = 'Z003'
AND stxl~tdspras = @sy-langu
INTO CORRESPONDING FIELDS OF TABLE @lt_stxl.CLEAR:lt_vbak_h[]."后续使用不到就清空"查询销售行的长文本
SELECT stxl~tdobject,stxl~tdid,stxl~tdname,stxl~clustr,stxl~clustdFROM stxlINNER JOIN @lt_vbak AS l ON l~tdname EQ stxl~tdname "之所以没有使用FOR ALL 是因为@DATA(LT_VBAK) 产生的TDNAME字段与 STXL~TDNAME 字段长度不一致WHERE stxl~tdobject = 'VBBP'AND stxl~tdid = 'Z007'AND stxl~tdspras = @sy-languAPPENDING CORRESPONDING FIELDS OF TABLE @lt_stxl."解压文本
LOOP AT lt_stxl.CLEAR:lt_stxl_raw,lt_stxl_raw[], lt_tline,lt_tline[].lt_stxl_raw-clustr = lt_stxl-clustr.lt_stxl_raw-clustd = lt_stxl-clustd.APPEND lt_stxl_raw.IMPORT tline = lt_tline FROM INTERNAL TABLE lt_stxl_raw.LOOP AT lt_tline.ls_text-tdline = ls_text-tdline && lt_tline-tdline.ENDLOOP.ls_text-tdobject = lt_stxl-tdobject.ls_text-tdid = lt_stxl-tdid.ls_text-tdname = lt_stxl-tdname.IF ls_text-tdline NE ''.INSERT ls_text INTO TABLE lt_text.ENDIF.CLEAR ls_text.
ENDLOOP.SORT lt_text BY tdobject tdid tdname ."排序为二分法做准备
DELETE ADJACENT DUPLICATES FROM lt_text COMPARING tdobject tdid tdname."匹配到ALV内表中
LOOP AT lt_vbak INTO DATA(ls_vbak).CLEAR: ls_vbak-longtext ,ls_vbak-longtexth."先清空长文本"取值销售订单抬头长文本READ TABLE lt_text INTO ls_text WITH KEY tdobject = 'VBBK' tdid = 'Z003' tdname = ls_vbak-vbeln BINARY SEARCH.IF sy-subrc = 0.ls_vbak-longtexth = ls_text-tdline.ENDIF.CLEAR ls_text."销售行文本READ TABLE lt_text INTO ls_text WITH KEY tdobject = 'VBBP' tdid = 'Z007' tdname = ls_vbak-tdname BINARY SEARCH.IF sy-subrc = 0.ls_vbak-longtext = ls_text-tdline.ENDIF.CLEAR ls_text .MODIFY lt_vbak FROM ls_vbak.CLEAR:ls_vbak.
ENDLOOP.cl_demo_output=>display_data( lt_vbak ).
二、结果展示
系统里面很多地方都用到了长文本,这里就涉及到长文本的一系列操作:创建、修改、读取以及删除。
一、长文本创建:
"定义变量:DATA:it_tab TYPE STANDARD TABLE OF TLINE, “传入内容wa_tab LIKE LINE OF it_tab,g_name TYPE THEAD-TDNAME. ”文本对象名称"调用BAPI:CREATE_TEXTCALL FUNCTION 'CREATE_TEXT'EXPORTINGFID = 'Z002' "Text IDFLANGUAGE = '1' "语言代码FNAME = G_NAME "文本名称FOBJECT = 'VBBP' "文本对象SAVE_DIRECT = 'X' "直接保存标识TABLESFLINES = it_tabEXCEPTIONSNO_INIT = 1NO_SAVE = 2OTHERS = 3.
二、长文本修改:调用BAPI与创建相同。
传值为已有内容
三、长文本读取:
"参数定义与创建长文相同"调用BAPI:READ_TEXTCALL FUNCTION 'READ_TEXT'EXPORTINGCLIENT = SY-MANDTID = 'Z002' "Text IDLANGUAGE = 'E' "语言代码NAME = g_name "文本名称OBJECT = 'VBBP' "文本对象TABLESLINES = IT_TABEXCEPTIONSID = 1LANGUAGE = 2NAME = 3NOT_FOUND = 4OBJECT = 5REFERENCE_CHECK = 6WRONG_ACCESS_TO_ARCHIVE = 7OTHERS = 8.
四、删除长文本
"调用BAPI:DELETE_TEXTCALL FUNCTION 'DELETE_TEXT'EXPORTINGCLIENT = SY-MANDTID = 'Z013'LANGUAGE = 'Z'NAME = G_NAMEOBJECT = 'VBBK'SAVEMODE_DIRECT = 'X'EXCEPTIONSNOT_FOUND = 1OTHERS = 2.
通过调试发现,删除SO抬头长文本,系统调用如下代码段:
ABAP 取抬头、项目长文本(使用类方法,比‘read_text‘方便快捷)_abap 销售订单抬头文本-CSDN博客
ABAP 取抬头、项目长文本(使用类方法,比‘read_text‘方便快捷)_abap 销售订单抬头文本-CSDN博客
<长文本、二>SAP 通过BAPI创建、修改、读取以及删除_sap long text button-CSDN博客
x类型与xstring
类型 | 容量 | 含义 | ||
X | 1~524,287 bytes | 1 byte | 十六进制字符 0-9, A-F具体的范围为:00~FF 类型X是十六进制类型,可表示内存字节实际内容,使用两个十六制字符表示一个字节中所存储的内容。但直接打印输出时,输出的还是赋值时字面意义上的值,而不是Unicode解码后的字符 如果未在 DATA 语句中指定参数<length>,则创建长度为 1 注:如果值是字母,则一定要大写,否则赋值不进 DATA: x2(2) TYPE x. | 十六进制的00 |
xstring | 不限定 | 由十六进制字符 0-9, A-F组成字符串。直接打印输出时,输出的还是赋值时字面意义上的值,而不是Unicode解码后的字符串 注:如果值是字母,则一定要大写,否则赋值不进 DATA : xs TYPE xstring . | 长度为0的空字符串‘‘ |
ABAP X类型 和 xstring类型_abap xstring-CSDN博客