天天看点

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.