ABAP——动态SQL

Posted 鲸与海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABAP——动态SQL相关的知识,希望对你有一定的参考价值。

效果:

代码:

************************************************************************
* Program Name      :
* Descriptions      :
* T-Code            :
* Updates Tables    :
* Input  Parameters :
* Output Parameters :
* Return Codes      :
* Special Logic     :
* Includes          :
************************************************************************
* Modification Log
************************************************************************
*   Date   Ver. Programmer   Descriptions
* -------- ---- ------------ -------------------------------------------
* 2020 3.9     xxx    Create
*
************************************************************************
REPORT  ZPPRTEST.
***********************************************************************
* Tables Definitions
************************************************************************
*TABLES:.
************************************************************************
* Data Definitions
************************************************************************

************************************************************************
* Includes Module
************************************************************************

************************************************************************
* Selection Screen
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.
PARAMETERS: p_num(2) TYPE n DEFAULT \'6\' OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

************************************************************************
* Initialization
************************************************************************
INITIALIZATION.

************************************************************************
* At Selection Screen
************************************************************************
AT SELECTION-SCREEN.

************************************************************************
* At Selection Screen Output
************************************************************************
AT SELECTION-SCREEN OUTPUT.

************************************************************************
* Report Format
************************************************************************
TOP-OF-PAGE.

END-OF-PAGE.
************************************************************************
* Main Process
************************************************************************
START-OF-SELECTION.
  PERFORM get_data.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
FORM get_data.
  DATA: l_vgw      LIKE plpo-vgw01,
        l_sql(500) TYPE c,
        l_num TYPE i,
        l_numc TYPE String.

*  l_sql = \'SUM( plpo~vgw01 + plpo~vgw02 )\'.

  DO p_num TIMES.
    l_num = l_num + 1.
    l_numc = l_num.
    IF l_num < 9.
      CONCATENATE l_sql \' plpo~vgw0\' l_numc \'+\' INTO l_sql.
    ELSE.
      CONCATENATE l_sql \' plpo~vgw\' l_numc \'+\' INTO l_sql.
    ENDIF.
  ENDDO.
  "去掉末尾+
  l_sql = SUBSTRING(  VAL = l_sql LEN = STRLEN( l_sql ) - 1 ) .
  CONCATENATE \'SUM(\' l_sql \' )\' INTO l_sql.

  SELECT (l_sql) INTO @l_vgw
            FROM plpo
            INNER JOIN plas ON  plpo~plnnr = plas~plnnr
            AND plpo~plnkn = plas~plnkn
            WHERE plas~loekz = @space
            AND plpo~plnnr = \'50033934\'
            AND plpo~vornr = \'0040\'.
  ENDSELECT.

*  SELECT SUM( plpo~vgw01 + plpo~vgw02 ) INTO @l_vgw
*            FROM plpo
*            INNER JOIN plas ON  plpo~plnnr = plas~plnnr
*            AND plpo~plnkn = plas~plnkn
*            WHERE plas~loekz = @space
*            AND plpo~plnnr = \'50033934\'
*            AND plpo~vornr = \'0040\'.

  cl_demo_output=>write( l_sql ).
  cl_demo_output=>write( l_vgw ).
  cl_demo_output=>display(  ).
ENDFORM.                "GET_DATA

 

以上是关于ABAP——动态SQL的主要内容,如果未能解决你的问题,请参考以下文章

动态SQL基础概念复习(Javaweb作业5)

MyBatis动态SQL标签用法

动态生成ABAP程序-资料

Mybatis -- 动态Sql概述动态Sql之<if>(包含<where>)动态Sql之<foreach>sql片段抽取

mybatis学习(39):动态sql片段

[mybatis]动态sql_sql_抽取可重用的sql片段