REPORT ztest001_xch. TABLES: makt. DATA where_tab(80) OCCURS 10 WITH HEADER LINE. DATA : wa_itab LIKE makt OCCURS 10 WITH HEADER LINE. PARAMETERS: s_trx LIKE makt-matnr OBLIGATORY, lan LIKE makt-spras OBLIGATORY. INITIALIZATION. MOVE 'EN' TO lan. START-OF-SELECTION. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = s_trx IMPORTING output = s_trx. CONCATENATE 'matnr like ''%' s_trx '%''' INTO where_tab. APPEND where_tab. CONCATENATE ' and SPRAS = ''' lan '''' INTO where_tab. APPEND where_tab. CLEAR where_tab. SELECT * FROM makt INTO TABLE wa_itab WHERE (where_tab). END-OF-SELECTION. LOOP AT wa_itab. WRITE:/ wa_itab-matnr,wa_itab-spras. ENDLOOP.
一些教训:单引号是特殊字符,如果要用到单引号,必须得转义,'',即两个单引号。SQL语句查询条件为字符时,需要加单引号。CONCATENATE ' and SPRAS = ''' lan '''' INTO where_tab. 实际上是三个字符串连接成一个字符串存放到内表中,and SPRAS = '变量',前面是一个字符串,and SPRAS = '',中间是一个变量 lan,后面是一个单引号'' ,所以合起来就是 CONCATENATE ' and SPRAS = ''' lan '''' INTO where_tab.
关于字符串的连接和单引号的使用,需要去体会和实践一下。这是关于在SAP中如何多条件语句的使用示例。
注意:WHERE (where_tab) 。括号必须紧跟着这个内表,不能有空格。