天天看點

ABAP-定義

ABAP [Field Symbol]

利用FIELD SYMBOL動态取表值:

ZFIELD_SYMBOL

PARAMETERS: dbtab TYPE c LENGTH 10,

rows TYPE i DEFAULT 100.

DATA dref TYPE REF TO data.

FIELD-SYMBOLS: <tab> TYPE ANY TABLE,

<wa> TYPE ANY,

<fs> TYPE ANY.

TRY.

CREATE DATA dref TYPE STANDARD TABLE OF (dbtab)

WITH NON-UNIQUE DEFAULT KEY.

ASSIGN dref->* TO <tab>.

SELECT *

FROM (dbtab) UP TO rows ROWS

INTO TABLE <tab>.

LOOP AT <tab> ASSIGNING <wa>.

DO.

ASSIGN COMPONENT sy-index

OF STRUCTURE <wa> TO <fs>.

IF sy-subrc = 0.

WRITE / <fs>.

ELSE.

EXIT.

ENDIF.

ENDDO.

ULINE.

ENDLOOP.

CATCH cx_sy_create_data_error.

WRITE 'Wrong Database!'.

ENDTRY.

Field-symbol 樣例:

Full type specification

REPORT demo_field_symbols_type .

DATA: BEGIN OF line,

col1(1) TYPE c,

col2(1) TYPE c VALUE 'X',

END OF line.

FIELD-SYMBOLS <fs> LIKE line.

ASSIGN line TO <fs>.

MOVE <fs>-col2 TO <fs>-col1.

WRITE: <fs>-col1, <fs>-col2.

--------------------------------------------------------------------------------

Forcing structures

REPORT demo_field_symbols_structure .

DATA: wa(10) TYPE c VALUE '0123456789'.

DATA: BEGIN OF line1,

col1(3) TYPE c,

col2(2) TYPE c,

col3(5) TYPE c,

END OF line1.

DATA: BEGIN OF line2,

col1(2) TYPE c,

col2 TYPE sy-datum,

END OF line2.

* obsolete -------------------------------------------------------------

FIELD-SYMBOLS: <f1> STRUCTURE line1 DEFAULT wa,

<f2> STRUCTURE line2 DEFAULT wa.

* correct --------------------------------------------------------------

FIELD-SYMBOLS <f3> LIKE line1.

ASSIGN wa TO <f3> CASTING.

FIELD-SYMBOLS <f4> LIKE line2.

ASSIGN wa TO <f4> CASTING.

* ----------------------------------------------------------------------

WRITE: / <f1>-col1, <f1>-col2, <f1>-col3,

/ <f2>-col1, <f2>-col2.

SKIP.

WRITE: / <f3>-col1, <f3>-col2, <f3>-col3,

/ <f4>-col1, <f4>-col2.

--------------------------------------------------------------------------------

Static assign

REPORT demo_field_symbols_stat_assign .

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.

DATA: text(20) TYPE c VALUE 'Hello, how are you?',

num TYPE i VALUE 5,

BEGIN OF line1,

col1 TYPE f VALUE '1.1e+10',

col2 TYPE i VALUE '1234',

END OF line1,

line2 LIKE line1.

ASSIGN text TO <f1>.

ASSIGN num TO <f2>.

DESCRIBE FIELD <f1> LENGTH <f2>.

WRITE: / <f1>, 'has length', num.

ASSIGN line1 TO <f1>.

ASSIGN line2-col2 TO <f2>.

MOVE <f1> TO line2.

ASSIGN 'LINE2-COL2 =' TO <f1>.

WRITE: / <f1>, <f2>.

Assign with offset

REPORT demo_field_symbols_stat_as_off .

FIELD-SYMBOLS <fs> TYPE ANY.

DATA: BEGIN OF line,

string1(10) VALUE '0123456789',

string2(10) VALUE 'abcdefghij',

END OF line.

WRITE / line-string1+5.

ASSIGN line-string1+5 TO <fs>.

WRITE / <fs>.

ASSIGN line-string1+5(*) TO <fs>.

WRITE / <fs>.

REPORT demo_field_symbols_stat_as_of2 .

FIELD-SYMBOLS <fs> TYPE ANY.

DATA: BEGIN OF line,

a TYPE c VALUE '1', b TYPE c VALUE '2',

c TYPE c VALUE '3', d TYPE c VALUE '4',

e TYPE c VALUE '5', f TYPE c VALUE '6',

g TYPE c VALUE '7', h TYPE c VALUE '8',

END OF line,

off TYPE i,

len TYPE i VALUE 2.

DO 2 TIMES.

off = sy-index * 3.

ASSIGN line-a+off(len) TO <fs>.

<fs> = 'XX'.

ENDDO.

DO 8 TIMES.

off = sy-index - 1.

ASSIGN line-a+off(1) TO <fs>.

WRITE <fs>.

ENDDO.

--------------------------------------------------------------------------------

Dynamic assign

REPORT demo_field_symbols_dynami_as_2 .

TABLES sbook.

DATA: name1(20) TYPE c VALUE 'SBOOK-FLDATE',

name2(20) TYPE c VALUE 'NAME1'.

FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN TABLE FIELD (name1) TO <fs>.

WRITE: / 'SY-SUBRC:', sy-subrc.

ASSIGN TABLE FIELD (name2) TO <fs>.

WRITE: / 'SY-SUBRC:', sy-subrc.

--------------------------------------------------------------------------------

Assigning field symbols

REPORT demo_field_symbols_dynami_as_3 .

DATA: BEGIN OF s,

a TYPE c VALUE '1', b TYPE c VALUE '2', c TYPE c VALUE '3',

d TYPE c VALUE '4', e TYPE c VALUE '5', f TYPE c VALUE '6',

g TYPE c VALUE '7', h TYPE c VALUE '8',

END OF s.

DATA off TYPE i.

FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN s-a TO <fs>.

DO 4 TIMES.

off = sy-index - 1.

ASSIGN <fs>+off(1) TO <fs>.

WRITE <fs>.

ENDDO.

Assigning a structure by component

REPORT demo_field_symbols_assign_comp .

DATA: BEGIN OF line,

col1 TYPE i VALUE '11',

col2 TYPE i VALUE '22',

col3 TYPE i VALUE '33',

END OF line.

DATA comp(5) TYPE c VALUE 'COL3'.

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE ANY, <f3> TYPE ANY.

ASSIGN line TO <f1>.

ASSIGN comp TO <f2>.

DO 3 TIMES.

ASSIGN COMPONENT sy-index OF STRUCTURE <f1> TO <f3>.

WRITE <f3>.

ENDDO.

ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.

WRITE / <f3>.

--------------------------------------------------------------------------------

Casting with field symbol type

REPORT demo_field_symbols_casting.

TYPES: BEGIN OF t_date,

year(4) TYPE n,

month(2) TYPE n,

day(2) TYPE n,

END OF t_date.

FIELD-SYMBOLS <fs> TYPE t_date.

ASSIGN sy-datum TO <fs> CASTING.

WRITE / sy-datum.

SKIP.

WRITE: / <fs>-year , / <fs>-month, / <fs>-day.

--------------------------------------------------------------------------------

Casting with explicit type

REPORT demo_field_symbols_casting_typ.

TYPES: BEGIN OF t_date,

year(4) TYPE n,

month(2) TYPE n,

day(2) TYPE n,

END OF t_date.

FIELD-SYMBOLS: <fs> TYPE ANY,

<f> TYPE n.

ASSIGN sy-datum TO <fs> CASTING TYPE t_date.

WRITE / sy-datum.

SKIP.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>.

IF sy-subrc &lt;&gt; 0.

EXIT.

ENDIF.

WRITE / <f>.

ENDDO.

Casting with predefined data types

REPORT demo_field_symbols_assign_type .

DATA txt(8) TYPE c VALUE '19980606'.

DATA mytype(1) VALUE 'X'.

FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN txt TO <fs>.

WRITE / <fs>.

SKIP.

* obsolete -------------------------------------------------------------

ASSIGN txt TO <fs> TYPE 'D'.

WRITE / <fs>.

ASSIGN txt TO <fs> TYPE mytype.

WRITE / <fs>.

SKIP.

* correct --------------------------------------------------------------

ASSIGN txt TO <fs> CASTING TYPE d.

WRITE / <fs>.

ASSIGN txt TO <fs> CASTING TYPE (mytype).

WRITE / <fs>.

--------------------------------------------------------------------------------

Casting decimla places

REPORT demo_field_symbols_assign_deci .

DATA: pack1 TYPE p DECIMALS 2 VALUE '400',

pack2 TYPE p DECIMALS 2,

pack3 TYPE p DECIMALS 2.

FIELD-SYMBOLS: <f1> TYPE ANY ,

<f2> TYPE ANY.

WRITE: / 'PACK1', pack1.

SKIP.

* obsolete -------------------------------------------------------------

ASSIGN pack1 TO <f1> DECIMALS 1.

WRITE: / '<f1> ', <f1>.

pack2 = <f1>.

WRITE: / 'PACK2', pack2.

ASSIGN pack2 TO <f2> DECIMALS 4.

WRITE: / '<f2> ', <f2>.

pack3 = <f1> + <f2>.

WRITE: / 'PACK3', pack3.

<f2> = '1234.56789'.

WRITE: / '<f2> ', <f2>.

WRITE: / 'PACK2', pack2.

SKIP.

* correct --------------------------------------------------------------

ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.

WRITE: / '<f1> ', <f1>.

pack2 = <f1>.

WRITE: / 'PACK2', pack2.

ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4.

WRITE: / '<f2> ', <f2>.

pack3 = <f1> + <f2>.

WRITE: / 'PACK3', pack3.

<f2> = '1234.56789'.

WRITE: / '<f2> ', <f2>.

WRITE: / 'PACK2', pack2.

Data areas for field symbols

REPORT demo_field_symbols_assign_err .

DATA: text1(10) TYPE c, text2(10) TYPE c, text3(5) TYPE c.

FIELD-SYMBOLS <fs> TYPE ANY.

DO 100 TIMES. "Runtime-Error!

ASSIGN text1+sy-index(1) TO <fs>.

ENDDO.

--------------------------------------------------------------------------------

Data references

REPORT demo_data_reference.

TYPES: BEGIN OF t_struct,

col1 TYPE i,

col2 TYPE i,

END OF t_struct.

DATA: dref1 TYPE REF TO data,

dref2 TYPE REF TO data.

FIELD-SYMBOLS: <fs1> TYPE t_struct,

<fs2> TYPE i.

CREATE DATA dref1 TYPE t_struct.

ASSIGN dref1-&gt;* TO <fs1>.

<fs1>-col1 = 1.

<fs1>-col2 = 2.

dref2 = dref1.

ASSIGN dref2-&gt;* TO <fs2> CASTING.

WRITE / <fs2>.

GET REFERENCE OF <fs1>-col2 INTO dref2.

ASSIGN dref2-&gt;* TO <fs2>.

WRITE / <fs2>.

Transparent tableThere is a physical table on the database for each transparent table. The names of the physical tables and the logical table definition in the ABAP/4 Dictionary correspond.All business data and application data are stored in transparent tables.透明表對于每一張透明表,在資料庫中都會存在一張實體表與之對應。實體表名和在ABAP/4字典中的邏輯表名一緻。所有業務資料和應用資料都存儲在透明表中。Pooled tablePooled tables can be used to store control data (e.g. screen sequences, program parameters or temporary data). Several pooled tables can be combined to form a table pool. The table pool corresponds to a physical table on the database in which all the records of the allocated pooled tables are stored.池表(共享表)池表可以用來存儲控制資料(例如:螢幕順序,程式參數,或者臨時資料)。多個池表可以組合成一個表池。這個表池與資料庫中的一個實體表對應,在這個實體表中儲存了所有池表的資料。Cluster tableCluster tables contain continuous text, for example, documentation. Several cluster tables can be combined to form a table cluster. Several logical lines of different tables are combined to form a physical record in this table type. This permits object-by-object storage or object-by-object access. In order to combine tables in clusters, at least parts of the keys must agree. Several cluster tables are stored in one corresponding table on the database.簇表(聚集表)簇表包含連續文本,例如 文檔。 多個簇表可以組合成一個表簇。 不同表的多條邏輯行可以組合成一條該表類型的實體記錄。簇允許object-by-object存儲 或者 object-by-object通路。 為了在簇中組合表,至少關鍵字的部分需要滿足。 多個簇表的資料存儲在資料庫中的唯一一個對應的實體表中。什麼是簇表?我們可以很直覺的了解為,在DATABASE (資料層),除了我們定義的主鍵,然後把其他的内容都當成表格的一個字段存起來。也就是說這個表的結構可以了解為:主鍵+ 一個存儲功能的字段的 組合。然而在其中還會有PAGENO的這個字段,這就是說,一行空間不夠(32KB)用,在主鍵相通的情況下,繼續細分,用來将屬于一個主鍵的内容聯系起來的字段1、2、3.。。。。表簇和簇表的差別和聯系?表簇,是該表在DATABASE的存儲結構,簇表,是該表在SAP DICTIONARY展現在我們眼前的結構。Cluster tables and Pooled tables have many to one relationship with the underlying database.A table pool corresponds to a table in the database in which all records from the pooled tables assigned to it are stored.Several logical data records from different cluster tables can be stored together in one physical record in a table cluster.· A pooled table cannot have the name having more than 10 characters.· All the key fields of the pooled table must be of character data type.· In pooled tables, the maximum length of the key field/data fields should not exceed the length of varkey/vardata of the pool respectively.· In cluster table the records having the same key are stored in a single key in the cluster.· If there is an overflow of the data records a continuation record is created with the same table key.簇表和池表在資料路中都有多對一的關系。一個表池對應資料庫中一個實體表,在這個實體表中,存儲了所有池表的資料。多個不同的簇表的多條邏輯資料可以聯合存儲在表簇中的一條記錄中。池表表名不能多餘10位字元所有池表的關鍵字段必須是字元類型在池表中,關鍵字/普通字段的最大長度不得超過 表池 中對應字段 varkey/vardata 的長度擁有相同關鍵字的簇表記錄 存儲在簇中單個關鍵字下如果出現資料記錄溢出的情況,會在相同的表關鍵字下建立一條連續記錄簇表都是由透明表轉化成的,要建立簇表,首先就要建立一個透明表,這個透明表的顯示結構,就是我們想想在簇表中顯示的結構,然後在SE11 進去看觀看此透明表狀态并且處于修改狀态,點選EXTRAS -> CHANGEtable category. 選最後一個 (RADIO BUTTON) 複制簇表的選項,是個小對勾,這個點完了,系統會自動跳到SE11的界面,好像什麼也沒有發生。這時候我們去DELIVERY AND MAINTENANCE标簽下面看,就會出現了一個POOL/CLUSTER的文本框,填入我們建立的表簇。激活儲存。就搞定了。注意,這裡我們要在表簇中 手工将VARKEY 删掉,換成我們表的要顯示的主鍵, 表簇主鍵為我要顯示主鍵的子集。但是池表和簇表還是有差別的。INSERT 透明表 INTO 簇表。 隻要透明的簇表的主鍵都在透明表裡面就行。就是透明表主鍵多了也無所謂。INSERT 透明表 INTO 池表. 透明表和池表的主鍵必須相同的。

AP的三種内表

标準表:關鍵字為STANDARD TABLE, 系統為該表的每一行資料生成一個邏輯索引。填充标準表時,可以将資料附加在現有行之後,也可以插入到指定的位置,程式對内表行的尋址操作可通過關鍵字或索引進行。在對表進行插入、删除等操作時,各資料行在記憶體中的位置不變,系統僅重新排列各資料行的索引值。

排序表:關鍵字為 SORTED TABLE, 也具有一個邏輯索引,不同之處是排序表總是按其關鍵字升序排序後再進行存儲,其通路方式與标準表相同。

哈希表:關鍵字為 HASHED TABLE, 沒有索引,隻能通過關鍵字來通路。系統用雜湊演算法管理表中的資料,因而其尋址一個資料行的時間和表的行數無關。

除以上三種類型之外,還有一般性類型,即索引表 INDEX TABLE和人意表ANY TABLE. 一般性類型可以用于類型定義中,但不能用于聲明一個内表對象,因為它并沒有指明任何明确的表類型,因而系統無法确定對其操作方式。一般性類型還可以用于指明字段符号和接口參數的類型,以增加這些元素的适用性,其實際類型可能在運作期内才能夠确定。

Standard Table ?6?1 Processed mainly by Index Operation?6?1 Key is always non-unique?6?1 Can be filled very quickly
Sorted Table ?6?1 Access mainly by a Fixed Key (efficient)?6?1 Index operation seldom?6?1 The response time for key access is logarithmically proportional tothe number of table entries?6?1 Key can be unique or non-unique?6?1 Data is inserted as per key
Hashed Table ?6?1 Access only by key (fast)?6?1 Key must be unique?6?1 No index operation?6?1 The response time is independent of the number of table entries,and is constant, since the system access the table entries using ahash algorithm

Table選擇,以及性能優劣:

Slowest table accesses are sequential and grow linearly with n.

¨ READ standard table WITH (TABLE) KEY (no BINARY SEARCH)

¨ LOOP at standard/hashed table.

¨ READ sorted/hashed table WITH KEY (not TABLE KEY).

Faster accesses use a sorted index and have a weak dependence on n.

¨ READ standard table WITH (TABLE) KEY using BINARY SEARCH)

¨ READ sorted table WITH TABLE KEY.

¨ LOOP at sorted table.

Fastest access independent of n.

¨ READ hashed table WITH TABLE KEY.

¨ READ standard table WITH INDEX

¨ READ sorted table WITH INDEX.

Note: n = number of lines in the internal table

三種内表特點:

讀取方式:

ABAP-定義

NON-UNIQUE|UNIQUE :NON-UNIQUE|UNIQUE(非特有/特有)決定了内表中具有相同關鍵字的資料行是否可以重複出現(即如果指明為UNIQUE KEY,則通過表關鍵字能夠唯一确定内表的行,在程式中不能插入具有相同表關鍵字的多行資料條目),其中标準表不能用UNIQUE關鍵字,且無需特别制定NON-UNIQUE關鍵字;排序表兩者都可以;哈希表不能用NON-UNIQUE關鍵字,且必須指定UNIQUE關鍵字。

INITIAL SIZE n: n可以為0或者任意正數,除非系統管理者限定一個最大值。無論n值選擇為多大,都不會影響程式的正确性,但可能會影響程式效率。如果初始值n小于所需,系統會根據需要自動要求增加記憶體大小,這個過程需要占用一定的時間;如果n值過大,實際資料的行數少于定義,則會造成記憶體配置設定的浪費。

是以n值的選擇應該盡量接近于實際所需。如果n為0或者不指定,程式會為内表對象配置設定8KB大小的記憶體。是以,如果比較小,不要把該值設為0,避免浪費記憶體。

SAP中的通配符和轉義符

搜尋幫助中:

通配符: *、+

轉義符:#

OPEN SQL中:

通配符: %、_

轉義符可以使用ESCAPE關鍵字來定義,如:

SELECT SINGLE * FROM makt WHERE spras = 1 and maktx LIKE '%/_' ESCAPE '/' .

字元串操作關鍵字中:

通配符: *、+

轉義符:#(CP操作符專用)

如:

REPORT z_barry_test.

PARAMETERS: P_INPUT(10) TYPE C LOWER CASE.

IF P_INPUT CP '#S#A#P123'.

WRITE:/ 'YES'.

ELSE.

WRITE:/ 'NO'.

ENDIF.

OR:

REPORT z_barry_test.

PARAMETERS: p_input(10) TYPE c LOWER CASE.

DATA: r_compare TYPE RANGE OF text10 WITH HEADER LINE.

r_compare-sign = 'I'.

r_compare-option = 'CP'.

r_compare-low = '#S#A#P123'.

APPEND r_compare.

IF p_input IN r_compare.

WRITE:/ 'YES'.

ELSE.

WRITE:/ 'NO'.

ENDIF.