[英]Concatenating strings in SAP Query
I'm creating a BAPI for SAP R/3.我正在为 SAP R/3 创建 BAPI。 The equivalent in MSSQL of what I'm trying to write is this:
我正在尝试编写的 MSSQL 中的等价物是:
select
bkpf.BELNR,
bkpf.BUKRS,
bkpf.GJAHR,
bkpf.AWKEY
into
#tab
from
bkpf
where
exists ( select 1 from #n_tab n where CONCAT(n.BELNR, n.GJAHR) = bkpf.AWKEY )
;
But apparently Open Sql doesn't allow operations in queries.但显然 Open Sql 不允许在查询中进行操作。 So for what I researched, the table I want to "join" must be retrieved to an in memory table, create a new column in a loop doing the operation and the compare with the #tab table.
因此,对于我的研究,我想“加入”的表必须检索到内存表中,在循环中创建一个新列执行操作并与#tab 表进行比较。 But I'm struggling with the syntax.
但我在语法上挣扎。 What I have so far is something like this:
到目前为止,我所拥有的是这样的:
FUNCTION ZBAPI_TEST.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"----------------------------------------------------------------------
select
bkpf~BELNR
bkpf~BUKRS
bkpf~GJAHR
bkpf~AWKEY
into ITAB_BKPF
from bkpf.
loop at ITAB_BKPF.
ITAB_BKPF-chkey = CONCATENATE BELNR GJAHR.
modify ITAB_BKPF.
endloop.
ENDFUNCTION.
But I'm getting the following errors.但我收到以下错误。
Field "ITAB_BKPF" is unknown.
字段“ITAB_BKPF”未知。 It is neither in one of the specified tables nor defined by a "DATA" statement.
它既不在指定的表之一中,也不由“DATA”语句定义。
Field "ITAB_BKPF-GJAHR" is unknown.
字段“ITAB_BKPF-GJAHR”未知。 It is neither in one of the specified tables nor defined by a "DATA" statement.
它既不在指定的表之一中,也不由“DATA”语句定义。
Incorrect nesting: Before the statement "ENDFUNCTION", the control structure introduced by "SELECT" must be concluded with "ENDSELECT".
嵌套错误:在语句“ENDFUNCTION”之前,“SELECT”引入的控制结构必须以“ENDSELECT”结束。
Incorrect nesting: Before the statement "+END-OF-INCLUDE", the control structure introduced by "FUNCTION" must be concluded with "ENDFUNCTION".
嵌套错误:在语句“+END-OF-INCLUDE”之前,“FUNCTION”引入的控制结构必须以“ENDFUNCTION”结束。
There's clearly an open statement.显然有一个公开声明。 But I'm not really familiar with the language and don't know where the period is required or if I misplaced any closing statement.
但我对这门语言不太熟悉,不知道在哪里需要句号,或者我是否放错了任何结束语。 Other approach I saw online was with a construct
SELECT..ENDSELECT
:我在网上看到的其他方法是使用构造
SELECT..ENDSELECT
:
FUNCTION ZBAPI_TEST.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"----------------------------------------------------------------------
select
bkpf~belnr
bkpf~bukrs
bkpf~gjahr
bkpf~awkey
into corresponding fields
wa_bkpf
from bkpf.
wa_bkpf-chkey = concatenate belnr gjahr.
append wa_bkpf to itab_bkpf.
endselect.
ENDFUNCTION.
But this generate a new batch of errors:但这会产生一批新的错误:
Field "CORRESPONDING" is unknown.
字段“对应”未知。 It is neither in one of the specified tables nor defined by a "DATA" statement.
它既不在指定的表之一中,也不由“DATA”语句定义。 Field "WA_BKPF-CHKEY" is unknown.
字段“WA_BKPF-CHKEY”未知。 It is neither in one of the specified tables nor defined by a "DATA" statement.
它既不在指定的表之一中,也不由“DATA”语句定义。 Field "WA_BKPF" is unknown.
字段“WA_BKPF”未知。 It is neither in one of the specified tables nor defined by a "DATA" statement.
它既不在指定的表之一中,也不由“DATA”语句定义。
I suspect the solutions and examples I found online skip some part where they define some of the structures they use.我怀疑我在网上找到的解决方案和例子跳过了他们定义他们使用的一些结构的部分。 But I don't really know how to do it.
但我真的不知道该怎么做。 Can someone help?
有人可以帮忙吗?
EDIT: The final code looks like that:编辑:最终代码如下所示:
types: begin of t_bkpf,
belnr type belnr_d,
bukrs type bukrs,
gjahr type gjahr,
awkey type awkey,
chkey type string.
types: end of t_bkpf.
data: itab_bkpf type standard table of t_bkpf.
field-symbols: <wa> type t_bkpf.
select
BELNR
BUKRS
GJAHR
AWKEY
into corresponding fields of table ITAB_BKPF
from bkpf.
loop at ITAB_BKPF assigning <wa>.
CONCATENATE <wa>-BELNR <wa>-GJAHR into <wa>-chkey.
endloop.
manoftheyear.年度人物。
Let me know if this works for you:让我知道这是否适合您:
*** Definition of custom type.
TYPES: BEGIN OF ty_bkpf,
belnr TYPE bukrs,
bukrs TYPE belnr_d,
gjahr TYPE gjahr,
awkey TYPE awkey,
chkey TYPE string, " Replace with the type you need to use.
END OF ty_bkpf.
*** Definition of internal table of custom type.
DATA lt_bkpf TYPE STANDARD TABLE OF ty_bkpf.
*** Definition of field symbol (pointer) of custom type.
FIELD-SYMBOLS <lfs_bkpf> TYPE ty_bkpf.
*** Extraction of data from BKPF database table.
SELECT belnr bukrs gjahr awkey
FROM bkpf
INTO CORRESPONDING FIELDS OF TABLE lt_bkpf.
*** Checks if extraction was succesful.
IF sy-subrc IS INITIAL.
UNASSIGN <lfs_bkpf>.
*** Loop internal table...
LOOP AT lt_bkpf ASSIGNING <lfs_bkpf>.
*** ...and create value for field CHKEY.
CONCATENATE <lfs_bkpf>-belnr
<lfs_bkpf>-gjahr
INTO <lfs_bkpf>-chkey. " By using a pointer, there's no need to use MODIFY sentence.
ENDLOOP.
ENDIF.
Cheers.干杯。
You can use CONCAT directly on QUERY.您可以直接在 QUERY 上使用 CONCAT。 Example:
示例:
SELECT SINGLE CONCAT( CONCAT( a~name1, a~name2 ), a~name3 )
FROM ( ( adrc AS a
INNER JOIN j_1bbranch AS j ON a~addrnumber = j~adrnr )
INNER JOIN t001w AS t ON j~cgc_branch = t~j_1bbranch
AND j~bukrs = @i_postab-bukrs
AND t~werks = @e_postab-prctr+4(4) )
INTO @e_postab-company_name.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.