动态order-by子句的运行时错误(静态等效子句起作用)

时间:2019-02-07 09:03:45

标签: sql sql-order-by abap dynamic-sql

问题是,对于动态排序依据子句,除了abap中的硬编码排序依据子句,是否还会触发其他任何检查。

我在abap中写了一条sql语句。该语句汇总特定年份的条目计数。然后,它根据条目的类型对计数进行分组。 之后,我尝试进行动态排序。出于某种原因,这会导致错误:“解析器产生了错误:包含IN_YEAR的表达式不是GROUP-BY表达式。”

问题在于,在对排序标准进行硬编码时,order-by子句起作用。当我尝试动态执行操作时,不会。

但是我必须动态地进行排序,因为稍后排序标准将通过oData(it_order)来自前端。

DATA:    lv_cYear      TYPE NUMC4,
         lv_lYear      TYPE NUMC4,
         lv_2YearsAgo  TYPE NUMC4,

         lv_order_by   TYPE string.

GET TIME.
lv_cYear = sy-datum+0(4).
LV_LYEAR = LV_CYEAR - 1.
LV_2YEARSAGO = LV_CYEAR - 2.

*later lv_order_by should be filled dynamically
lv_order_by = 'RRC2019 DESCENDING, RRC2018 DESCENDING'.    

SELECT      
   type_name AS type,
   SUM( CASE WHEN in_year eq @lv_cYear THEN record_count END ) AS RRC2019,
   SUM( CASE WHEN in_year eq @lv_lyear THEN record_count END ) AS RRC2018,
   SUM( CASE WHEN in_year eq @LV_2YEARSAGO THEN record_count END ) AS RRC2017
FROM entryTable
   GROUP BY type_name
   HAVING SUM( CASE WHEN in_year eq @lv_cYear THEN record_count END ) IN @ls_in_2019_range-select_options
   AND SUM( CASE WHEN in_year eq @lv_lyear THEN record_count END ) IN @ls_in_2018_range-select_options
   AND SUM( CASE WHEN in_year eq @LV_2YEARSAGO THEN record_count END ) IN @ls_in_2017_range-select_options
   ORDER BY (lv_order_by)
   INTO CORRESPONDING FIELDS OF TABLE @et_entityset.

如果尝试运行此错误,则会发生错误“解析器产生错误:包含IN_YEAR的表达式不是GROUP-BY表达式”。

如果将“ ORDER BY(lv_order_by)”替换为“ ORDER BY RRC2019 DESCENDING,RRC2018 DESCENDING”,则它起作用。

3 个答案:

答案 0 :(得分:2)

我使用了@Jagger的程序,并验证了它在7.50下可以正常工作并且从7.51开始中断。

这应该是一个错误。

更新: 请应用注释2753729

答案 1 :(得分:1)

我只是让自己准备一个可编译的工作示例,它似乎与您所写的相反。我唯一更改的是INTO TABLE或(在您的情况下)INTO CORRESPONDING FIELDS OF TABLE @et_entityset的位置。

下面的示例编译没有问题,也不会生成任何运行时异常。

REPORT YYY.

DATA:    lv_cYear      TYPE NUMC4,
         lv_lYear      TYPE NUMC4,
         lv_2YearsAgo  TYPE NUMC4,

         lv_order_by   TYPE string,
         ls_in_2019_range TYPE RANGE OF cosp-wtg001,
         ls_in_2018_range TYPE RANGE OF cosp-wtg001,
         ls_in_2017_range TYPE RANGE OF cosp-wtg001.

GET TIME.
lv_cYear = sy-datum+0(4).
LV_LYEAR = LV_CYEAR - 1.
LV_2YEARSAGO = LV_CYEAR - 2.

*later lv_order_by should be filled dynamically
lv_order_by = 'RRC2019 DESCENDING, RRC2018 DESCENDING'.

SELECT
   lednr AS type,
   SUM( CASE WHEN GJAHR eq @lv_cYear THEN wtg001 END ) AS RRC2019,
   SUM( CASE WHEN GJAHR eq @lv_lyear THEN wtg001 END ) AS RRC2018,
   SUM( CASE WHEN GJAHR eq @LV_2YEARSAGO THEN wtg001 END ) AS RRC2017
FROM cosp
   INTO TABLE @DATA(et_entityset)
   GROUP BY lednr
   HAVING SUM( CASE WHEN gjahr eq @lv_cYear THEN wtg001 END ) IN @ls_in_2019_range
   AND SUM( CASE WHEN gjahr eq @lv_lyear THEN wtg001 END ) IN @ls_in_2018_range
   AND SUM( CASE WHEN gjahr eq @LV_2YEARSAGO THEN wtg001 END ) IN @ls_in_2017_range
   ORDER BY (lv_order_by).

   BREAK-POINT.

答案 2 :(得分:1)

以下是在所有最近的基于ABAP的系统上编译的示例(使用SAPBC_DATA_GENERATOR程序填充SFLIGHT表):

<link rel="schema.DCTERMS" href="http://purl.org/dc/terms/" />
<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
<meta name="DC.contributor" content="Confraria, João" />
<meta name="DC.contributor" content="Medeiros, Rui" />
<meta name="DC.creator" content="Lopes, Ana Margarida Balseiro de Sousa" />
<meta name="DCTERMS.dateAccepted" content="2014-07-21T09:41:57Z" scheme="DCTERMS.W3CDTF" />
<meta name="DCTERMS.available" content="2014-07-21T09:41:57Z" scheme="DCTERMS.W3CDTF" />
<meta name="DCTERMS.issued" content="01-04-2014" scheme="DCTERMS.W3CDTF" />
<meta name="DC.identifier" content="http://hdl.handle.net/10400.14/14915" scheme="DCTERMS.URI" />
<meta name="DC.language" content="por" xml:lang="por" scheme="DCTERMS.RFC1766" />
<meta name="DC.rights" content="openAccess" xml:lang="por" />
<meta name="DC.title" content="Falhas do estado na regulação das parcerias público-privadas no setor rodoviário" xml:lang="por" />
<meta name="DC.type" content="masterThesis" xml:lang="por" />
<meta name="DC.identifier" content="201343371" />
<meta name="DC.subject" content="Domínio/Área Científica::Ciências Sociais::Direito" />

在以下2个系统上,我重现了该行为(它与静态ORDER BY子句,与动态ORDER BY子句的简短转储一起工作):

  • 使用SYBASE ASE的开发人员版7.52 SP 1
  • 具有ABAP 7.52 SP 0的S / 4HANA

毫无疑问,某个地方存在错误,但是我找不到SAP注释来纠正此问题。在SAP支持部门开票会更好。