1. 程式人生 > >【轉載】 ABAP SELECT-INTO用法 SELECT @ (AT) 新語法 SELECT * INTO DATA(@IT_ITAB)

【轉載】 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 [email protected] 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 
版權宣告:本文為博主原創文章,轉載請附上博文連結!