Skip to Content
avatar image
Former Member

Inline-declared table holding results of a SQL Query. Can it be used inside subroutines?

Hello experts,

I have been enjoying the features of ABAP 7.40 very much when it comes to making SQL queries without having to worry about time-consuming type declarations.

It's great to be able to do things like this (an example from Horst Keller's blog entry on host expressions):

SELECT carrid, connid, cityfrom, cityto
FROM spfli
WHERE carrid =
@( VALUE spfli-carrid( carriers[ KEY name
carrname = name ]-carrid
OPTIONAL ) )
INTO TABLE @DATA(result).

My question is, let's say I make this SQL, and then I want to use the newly-created internal table result as a parameter for a subroutine. The problem is, this table has been created inline without a type. Is there a way to pass it to a subroutine in a way that I can loop it, access the fields directly, etc?

Thanks a lot guys

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Apr 19, 2017 at 04:05 PM

    What do you mean with

    "The problem is, this table has been created inline without a type."

    Of course the table has a type. Check the documentation, how the type is derived. The compiler knows the type as it would be declared with DATA. In fact an inline declaration is kinda short form of DATA.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 20, 2017 at 12:26 PM

    No, that's the limit of inline declarations, you can't declare a parameter typed according to this declaration. You have to do it the old way. But that would be nice that the ABAP team implements this kind of feature in a next kernel :)

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 19, 2017 at 04:19 PM

    Hello Horst, congratulations for your blog, I love it.

    I actually thought the table was dynamically created. Maybe I should reformulate the question: is there a way for a subroutine to access the static type that is created on the inline? Even if it's static, it's hidden to the subroutines, right? Correct me if I'm wrong but I can't just pass that table to a subroutine without an explicit table type and expect to access the fields or loop the contents.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Horst,

      I read the documentation and now I see that the target inline declaration is derived from the results set, which makes sense, of course. Thank you for pointing out that subroutines are obsolete! I still see them pretty often at work, that's why I still don't feel like it.

      Anyway, if I pass the inline results table from the SQL as a parameter to a method, I still would have to pass it as a generic type like TYPE ANY TABLE, etc. right? If the type was statically derived by the compiler on the inline declaration, why the generic typing when it comes to parameters? Can't the compiler infer the type like in the inline declaration?

      I personally think that inlines combined with the new constructor expressions make programming in ABAP a breeze, and more fun compared to the "traditional way" of declaring types and helpers till you drop. I think it would be great to be able to inline SQL result tables and avoid the mechanical part of painstakingly defining a structure type for a 4 table join in order to pass it to a method with full static typing. Dynamic typing pretty much ends the immediacy of using the inline, right? And it's just plain wrong to use generic typing when full typing is possible if I just "manually" declare a type.

      Just my opinion. As you probably can tell, I think the declaration of types is plain boring, heheh

  • avatar image
    Former Member
    Apr 20, 2017 at 03:12 PM

    Sandra, I agree. It would be great to have on some future version of ABAP. If the compiler can derive the type of an internal table from a SQL result set, it should also be able to derive the type of an inline-declared internal table passed as a method parameter. It's the only feature I miss from inline variables, and I use them a lot ;)

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 20, 2017 at 04:46 PM

    Inline declarations create always "local" data. You cannot refer to local data types in global interfaces.

    What you can do of course, is referring them locally, but normally you don't want to do that ...

    SELECT *
      FROM scarr
      INTO TABLE @DATA(result).
    ...
    DATA line LIKE LINE OF result.
    ...
    FORM dang USING para LIKE result.
    ENDFORM
    

    Here we have a parameter typed according to an inline declaration.

    Add comment
    10|10000 characters needed characters exceeded