【转载】 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


... 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 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ABAP中的SELECT语句用于从数据库中选择数据。SELECT SINGLE语句用于选择一条数据,如果根据查询条件可以得到多条数据,则必须使用ENDSELECT来结束语句。然而,应尽量避免使用SELECT SINGLE,因为在ABAP语法检查中会有警告。可以使用其他方法来代替,例如使用SELECT ... INTO TABLE来选择数据并存储在内表中。\[1\] 在ABAP中,还可以使用SELECT语句结合CASE语句来进行条件选择。例如,可以使用SELECT ... SUM ... CASE ... END来计算某个字段的总和,并根据条件进行加减操作。\[2\] 在使用SELECT语句时,需要注意以下几点: 1. 在使用前必须判断内表参数是否非空,否则SELECT语句相当于没有WHERE条件限制,将取出表中所有数据。 2. 搜索出的数据必须包含该表的全部主键,否则可能无法精确匹配。 3. 使用内表作为条件时,内表不应太大,因为每行数据都会增加一个WHERE条件,太多的条件会影响效率。\[3\] 总之,ABAP中的SELECT语句是用于从数据库中选择数据的重要语句,可以根据需要使用不同的语法结构来实现不同的查询需求。 #### 引用[.reference_title] - *1* [ABAP数据库操作系列之操作语句讲解Select](https://blog.csdn.net/qq_53645728/article/details/129233177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ABAP SELECT SUM函数用法](https://blog.csdn.net/qq_40223617/article/details/126561381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SAP-ABAP-SELECT语法SQL语法详解](https://blog.csdn.net/Qunending/article/details/126656678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值