Skip to Content
0

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

Apr 19, 2017 at 03:54 PM

144

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

5 Answers

Horst Keller
Apr 19, 2017 at 04:05 PM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
Sandra Rossi Apr 20, 2017 at 12:26 PM
1

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 :)

Show 3 Share
10 |10000 characters needed characters left characters exceeded

"you can't declare a parameter typed according to this declaration."

Yes you can, but only within the natural limits of it's visibility.

0

Are you talking about doing the inline declaration in the global scope? Okay, it's then theorically possible but I guess the inline declarations are usually done locally, so ... or is there something I didn't understand?

0

See the example in the answer given below, 'tworks only locally

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

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.

Show 2 Share
10 |10000 characters needed characters left characters exceeded

But as I said above, it has a well known table type. It works exactly as if you would write a DATA statement with the type described in the documentation in front of the SELECT statement. And you can pass it to a method as any internal table. Regard inline declarations simply as short cuts.

0
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

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

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 ;)

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Apr 20, 2017 at 04:46 PM
0

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.

Share
10 |10000 characters needed characters left characters exceeded