Skip to Content
avatar image
Former Member

Dynamic SQL SELECT

Hello, this is my first question in a forum:

Can I make a select like this?

SELECT RACCT HSL01 + HSL02 + HSL03 + HSL04 AS TOTAL

FROM glt0

INTO TABLE i_tab.

I think it's not possible because it give me a lot of errors. Thanks in advance.

Esther

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Best Answer
    avatar image
    Former Member
    May 10, 2006 at 08:34 AM

    Hi Albert,

    Welcome to SDN..........

    It is not possible to add fields at select statement. To achiever your requirement, collect data into internal table and sum-up required fields.

    The code for this is as follows::

    types: begin of ty_tab,

    racct like glto-racct,

    hsl01 like glto-hsl01,

    hsl02 like glto-hsl02,

    hsl03 like glto-hsl03,

    hsl04 like glto-hsl04,

    end of ty_tab.

    types: begin of ty_tab_sum,

    racct like glto-racct,

    sum like glto-hsl04,

    end of ty_tab_sum.

    data:

    i_tab like standard table of ty_tab with header line,

    i_tab_sum like standard table of ty_tab_sum with header line.

    data:

    wa_tab type ty_tab.

    select racct hsl01 hsl02 hsl03 hsl04

    from glto

    intotable i_tab.

    if not i_tab[] is not initial.

    loop at i_tab into wa_tab.

    i_tab_sum-racct = wa_tab-racct.

    i_tab_sum-sum = wa_tab-hsl01 + wa_tab-hsl02 + wa_tab-hsl03 + wa_tab-hsl04.

    append i_tab_sum.

    clear wa_tab.

    endloop.

    endif.

    Thanks,

    Vinay

    Note: Plz reward points if it is helpful

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2006 at 07:51 AM

    Hi,

    Do u want to add those field values? U cannot do like that.

    Get the field data into the itab, then do the addition.

    If found useful, please award points.

    Thanks,

    Bharadwaj

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2006 at 08:37 AM

    Hi albert

    i am providing here a simple program just study it you will get the idea.

    DATA: BEGIN OF LINE,

    COL1 TYPE C,

    COL2 TYPE I,

    COL3 TYPE I,

    END OF LINE.

    DATA ITAB LIKE HASHED TABLE OF LINE

    WITH UNIQUE KEY COL1 COL2.

    LINE-COL1 = 'A'.

    DO 3 TIMES.

    LINE-COL2 = SY-INDEX.

    LINE-COL3 = SY-INDEX ** 2.

    INSERT LINE INTO TABLE ITAB.

    ENDDO.

    LINE-COL1 = 'B'.

    DO 3 TIMES.

    LINE-COL2 = 2 * SY-INDEX.

    LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

    INSERT LINE INTO TABLE ITAB.

    ENDDO.

    SORT ITAB.

    LOOP AT ITAB INTO LINE.

    WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

    AT END OF COL1.

    SUM.

    ULINE.

    WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

    SKIP.

    ENDAT.

    AT LAST.

    SUM.

    ULINE.

    WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

    ENDAT.

    ENDLOOP.

    The output is:

    A 1 1

    A 2 4

    A 3 9

    ________________________________

    A 6 14

    B 2 4

    B 4 16

    B 6 36

    ________________________________

    B 12 56

    ________________________________

    • 18 70

    The program creates a hashed table ITAB, fills it with six lines, and sorts it. In the LOOP - ENDLOOP block, the work area LINE is output for each loop pass. The first field of the table key, COL1, is used for control level processing. The total for all numeric fields is always calculated when the contents of COL1 change and when the system is in the last loop pass.

    Please reward for the same .

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2006 at 08:42 AM

    Use the following syntax

    SELECT 
    RACCT 
    HSL01
    HSL02
    HSL03
    HSL04
    FROM glt0
    INTO TABLE i_tab.

    After this just loop at ur key field, suppose racct and this sums at the end of each key field and moves the corresponsding racct sum value into sum_bom which has similar struc to i_tab

      LOOP AT i_tab.
    
        AT END OF racct.
    
          READ TABLE i_tab INDEX SY-TABIX.
          SUM.
          MOVE T_BOM TO sum_BOM.
          APPEND sum_BOM.
    
        ENDAT.
    
      ENDLOOP.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2006 at 08:46 AM

    Hi,

    There is no way to add the different fields of the databse in SQL statement. Fetch the data in internal table and then add the different fields of the table in a row.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2006 at 09:14 AM

    HI

    GOOD

    NO YOU CANT MAKE A SELECT STATEMENT LIKE THIS

    FIRST YOU SELECT ALL THE FIELDS THAT YOU WANT THAN YOU CAN DO THE CALCULATION.

    THANKS

    MRUTYUN

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2006 at 08:09 AM

    Albert,

    I don't think that's possible in ABAP.

    You will have to dump the data into a internal table and then do the summing.

    Regards,

    Ravi

    Note : Please close the thread if the question is answered and mark the helpful answers

    Add comment
    10|10000 characters needed characters exceeded