【转载】 ABAP SELECT-INTO用法 SELECT @ (AT) 新语法 SELECT * INTO DATA(@IT_ITAB)

READ TABLE LT_ITAB INTO DATA(LS_ITAB) INDEX 1.

看起来实际上是在运行时声明变量。

写程序时一直没有使用ABAP的新语法,今天记录一下新语法的使用,总结不全,想到什么就写什么,不喜勿喷!

找了个select,点了一下F1进去看看

先找个简单点的语法,因为程序要使用插入内表的操作,以前的步骤都是新建工作区,新建内表,再select,所以这里直接进去INTO看看新用法

想对比以前来说,这个SELECT - INTO 还是有点变化的,然后小试了一段代码

select matnr,ersda,ernam
  UP TO 20 rows
  from mara
  into TABLE @data(it_item).

调试进了一下,发现是有数据出现的

这种方法的实用性在于不用SELECT每一个表都进行一次内表的定义,大大节省了代码量,同时看着也舒服,而且SELECT语法也开始接近去SQL的语法。上面是插入内表的操作, 插入工作区也一样。

然后点回去看了一下官方的说明,很简单的一个使用,后面还有一个可选参数:PACKAGE SIZE n,下面英文是什么意思就不翻译了,大致理解一下意思是说,INTO WA或者ITAB的时候,前面要加上 @DATA,SELECT 后面跟着的字段就是要构成工作区或内表的结构。当要指定内表的大小时,可以使用 后面那个 PACKAGE SIZE n 参数。

Alternative 5

扫描二维码关注公众号,回复: 4179808 查看本文章


... INTO TABLE @DATA(itab) [PACKAGE SIZE n] 


Effect

Inline declaration of the target area. Thedeclaration operatorDATA must be prefixed with the escape character@. The data type of the new data object is constructed in accordance with the structure of the results set defined afterSELECT and the number of database tables or views specified afterFROM.INTO @DATA(wa) declares a flat data object wa of this type;INTO TABLE @DATA(itab) declares astandard tableitab of this row type with an empty table key. The same applies to PACKAGE SIZE as when specifying an existing internal table. 


后面一个注意事项,注意第二点说的是,不能使用相同或自定义的列名,否则会直接报错。。。


The prerequisites for an online declaration are as follows:

The SELECT list must be specified statically.

The results set cannot have multiple columns with the same name. This can be bypassed using alias names.

In a multi-column results set, eachSQL expression and eachaggregate expression must have an alias name. 

后面这段也是注意事项,大概说明了这种语法的使用规则,大致和旧语法一样,不翻译了!


The data type of the declared data object wa or itab is determined as follows:

If the results set afterSELECT is defined using a single specified column col_spec for which no name can be identified, the data type ofwa or the row type ofitab is its elementary type.

If the results set afterSELECT is defined using a single specified column col_spec for which no name can be identified, the data type ofwa or the row type ofitab is a structure with a component, with its elementary type.

If the results set afterSELECT is defined using a single data_source~* or a list of multiple specified columnscol_spec, the data type ofwa or the row type of itab is a structure with elementary components. The data types of the components are the elementary types of the columns in the results set in the order defined there.

If the results set afterSELECT is defined using data_source1~*, data_source2~*, ..., the data type ofwa or the row type ofitab is a nested structure. There is a substructure with the name or alias name of the table or view for every table or viewdata_source1,data_source2, ... specified. The data types of the components of the substructures are the elementary types of the database tables or views in the order defined there.

If the results set afterSELECT is defined using *, the data type depends on the number of database tables or views specified afterFROM:
In reads from a database table dbtab, view view or cds_view, the data type of wa or the row type of itab is the same as in a definition of the results set usingdata_source~* (see above).
In reads from multiple database tables or views data_source1,data_source2, ... using ajoin, the data type ofwa or the row type of itab is the same as in a definition of the results set usingdata_source1~*, data_source2~*, ... (see above). 


The elementary data type of an elementary data object or an elementary component of a structure is constructed as follows:

For columns of database tables or views, the data type is taken from ABAP Dictionary.

For SQL expressions andaggregate expressions, the data type is their result type.

For a single host variable as an SQL expression, the data type is its ABAP type.
The names of the elementary components of a structure match the names of the associated columns from theresults set. Any alias names defined there are respected. 


先写一部分,毕竟是第一篇的博客!

总结一下:

  1.这种语法使用的范围仅限于系统已有的表内的字段,不支持自定义字段,如果是要自定义字段的,还是     要老老        实实的定义内表吧。除非用AS来定义。

  2.使用的时候,要注意他们的定义方法,INTO @DATA(工作区) 或 INTO TABLE @DATA(内表)。

  3.每个变量之间要用逗号隔开,例如matnr,ersda。

  4.WHERE条件后面需要用到变量的,也需要带上@。

  5.大量节省了代码量,能少写的就尽量不要多写,起码代码的格式看起来舒服。

有缺少的地方,以后再进行补上!
--------------------- 
作者:Crayon華健 
来源:CSDN 
原文:https://blog.csdn.net/u012727414/article/details/52849052 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/hubaichun/article/details/84226759
今日推荐