ABAP Help Document(26):11.5 ABAP File Interface

11.5 ABAP File Interface

          Physical and Logic file Name,使用默认定义Tcode:RZ11,DIR_HOME默认文件路径;

Tcode:FILE,定义Non-client-specific logical file names;

使用Function: FILE_GET_NAME,获取logic file name;

使用Function: FILE_LOGFILE_ALIAS_PBO, FILE_LOGFILE_ALIAS_PAI, and FILE_LOGFILE_ALIAS_F4,输入输出框,以及F4帮助;

类CL_ABAP_FILE_UTILITIES,提供应用服务器文件信息;

以下Statements组成ABAP文件接口:

1.OPEN DATASET

语法:

OPEN DATASET dset FOR access IN mode [position] [os_additions] [error_handling].

access: INPUT | OUTPUT | APPENDING | UPDATE

input打开文件读取,output打开文件写入,appending打开文件追加,不存在就创建,update打开文件修改;

mode:

{BINARY MODE}
| {TEXT MODE encoding [linefeed] }
| {LEGACY BINARY MODE [{BIG|LITTLE} ENDIAN] [CODE PAGE cp]}
| {LEGACY TEXT MODE [{BIG|LITTLE} ENDIAN] [CODE PAGE cp] [linefeed]} ... .

模式,二进制或文本模式;

encoding:编码;

ENCODING { DEFAULT
             | {UTF-8 [SKIPPING|WITH BYTE-ORDER MARK]}
             | NON-UNICODE } ... .

linefeed:行尾标记;

1. WITH NATIVE LINEFEED,根据操作系统,行尾标记; 

2. WITH SMART LINEFEED,

3. WITH UNIX LINEFEED,"LF"作为行尾标记;

4. WITH WINDOWS LINEFEED ,"CRLF"作为行尾标记;

[position]:

AT POSITION pos ... .

[os_additions]:

[TYPE attr]
[FILTER opcom] ... .

If attr contains the value "NT", the end-of-line is marked by "CRLF".

If attr contains the value "UNIX", the end-of-line is marked by "LF".

[error_handling]:

[MESSAGE msg]
[IGNORING CONVERSION ERRORS]
[REPLACEMENT CHARACTER rc]

MESSAGE:信息;

IGNORING CONVERSION ERRORS:忽略转换错误;

REPLACEMENT CHARACTER:替换字符;

示例:

FORM f_file_interface_1.
  DATA:lv_file TYPE string VALUE `test.dat`.
  DATA:lv_result TYPE string.
  DATA:lv_mess TYPE string.
  "打开dataset,写模式
  OPEN DATASET lv_file FOR OUTPUT
    IN TEXT MODE
    ENCODING DEFAULT
    WITH SMART LINEFEED.
  TRANSFER `1234567890` TO lv_file.
  "关闭dataset
  CLOSE DATASET lv_file.

  "更新模式
  OPEN DATASET lv_file FOR UPDATE
    IN TEXT MODE
    ENCODING DEFAULT
    WITH SMART LINEFEED
    AT POSITION 2.
  TRANSFER `ABCD` TO lv_file.
  CLOSE DATASET lv_file.

  "读模式
  OPEN DATASET lv_file FOR INPUT
    IN TEXT MODE
    ENCODING DEFAULT
    WITH SMART LINEFEED
    MESSAGE lv_mess.
  WHILE sy-subrc = 0.
    READ DATASET lv_file INTO lv_result.
    WRITE:/ lv_result.
  ENDWHILE.
  CLOSE DATASET lv_file.
ENDFORM.

2.TRANSFER

语法:

TRANSFER dobj TO dset [LENGTH len]  [NO END OF LINE].

len小于等于0不传输任何字符字节;

以文本方式打开,写入默认添加行尾标记;

len大于dobj字符字节,添加空白或十六进制0;

示例:

FORM f_file_interface2.
  DATA:lv_file TYPE string VALUE `test.dat`.
  DATA:lt_sflight TYPE TABLE OF sflight.
  DATA:ls_sflight LIKE LINE OF lt_sflight.
  FIELD-SYMBOLS:<fs_hex> TYPE x.
  DATA:lv_x TYPE x LENGTH 1024.
  "写入二进制
  OPEN DATASET lv_file FOR OUTPUT IN BINARY MODE.
  SELECT SINGLE * FROM sflight INTO ls_sflight.
  ASSIGN ls_sflight TO <fs_hex> CASTING.
  TRANSFER <fs_hex> TO lv_file.
  "TRANSFER <fs_hex> TO lv_file LENGTH 100 NO END OF LINE.
  CLOSE DATASET lv_file.

  "读取二进制
  OPEN DATASET lv_file FOR INPUT IN BINARY MODE.
  WHILE sy-subrc = 0.
    READ DATASET lv_file INTO lv_x.
    WRITE:/ lv_x.
  ENDWHILE.
  CLOSE DATASET lv_file.
ENDFORM.

3.READ DATASET

语法:

READ DATASET dset INTO dobj [MAXIMUM LENGTH mlen] [[ACTUAL] LENGTH alen].

示例:

FORM f_file_interface3.
  DATA:lv_file TYPE string VALUE `test.dat`.
  FIELD-SYMBOLS:<fs_hex> TYPE sflight.
  DATA:lv_hex TYPE x LENGTH 1024.
  DATA:lv_maxlen TYPE I.

  DESCRIBE FIELD <fs_hex> LENGTH lv_maxlen IN BYTE MODE.
  ASSIGN lv_hex TO <fs_hex> CASTING.
  "读取二进制
  OPEN DATASET lv_file FOR INPUT IN BINARY MODE.
  DO.
    READ DATASET lv_file INTO lv_hex MAXIMUM LENGTH lv_maxlen.
    IF sy-subrc = 0.
      WRITE:/ lv_maxlen,<fs_hex>-carrid,<fs_hex>-connid,<fs_hex>-fldate.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.
  CLOSE DATASET lv_file.
ENDFORM.

4.GET DATASET

语法:

GET DATASET dset [POSITION pos] [ATTRIBUTES attr].

pos当前位置

attr,Type Group:DSET,结构体dset_attributes,包含两项:fixed and changeable;

fixed结构:

indicator

Structure whose components mode, access_type, encoding, filter, and linefeed in attr contain the value "X" if the identically named components of the structure fixed are significant for the current file.

mode

Storage mode. "T":text files; "LT":legacy text files; "B":binary files;  "LB":legacy binary files.

access_type

Access mode. "I":reading; "O":writing; "A":appending; "U": editing.

encoding

Character format. "NON-UNICODE", "UTF-8".

filter

filter

linefeed

linefeed

changeable结构:

Component

Meaning

indicator

Structure whose components repl_char, conv_errors, code_page, endian, and linefeed_mode contain the value "X" in attr if the identically named components of the structure changeable are significant for the current file.

repl_char

相当于REPLACEMENT CHARACTER

conv_errors

相当于IGNORING CONVERSION ERRORS

code_page

相当于CODE PAGE

endian

"B":BIG ENDIAN; "L" :LITTLE ENDIAN;

linefeed_mode

"N":WITH NATIVE LINEFEED;

"S":WITH SMART LINEFEED;

"U": WITH UNIX LINEFEED;

"W":WITH WINDOWS LINEFEED;

示例:

FORM f_file_interface4.
  DATA:lv_file TYPE string VALUE `test.dat`.
  DATA:lv_attr TYPE dset_attributes.
  DATA:lv_pos TYPE I.
  "读取二进制
  OPEN DATASET lv_file FOR INPUT
    IN TEXT MODE
    ENCODING UTF-8
    WITH NATIVE LINEFEED.
  GET DATASET lv_file POSITION lv_pos ATTRIBUTES lv_attr.
  WRITE:/ lv_attr-fixed-mode,
          lv_attr-fixed-access_type,
          lv_attr-fixed-encoding,
          lv_attr-fixed-linefeed.
  CLOSE DATASET lv_file.
ENDFORM. 

5.SET DATASET

语法:

SET DATASET dset [POSITION {pos|{END OF FILE}}] [ATTRIBUTES attr].

设置dset参数。

 

6.TRUNCATE DATASET

语法:

TRUNCATE DATASET dset AT {CURRENT POSITION}|{POSITION pos}.

在指定位置截断文件;

 

7.CLOSE DATASET

语法:

CLOSE DATASET dset.

关闭dataset;

 

8.DELETE DATASET

语法:

CLOSE DATASET dset.

删除dataset;

 

11.6 Data Consistency

          数据一致性,SAP通过LUWS保持数据一致性。1.Database LUWS通过,Database Locks保证数据一致性;2. SAP LUWS通过SAP locks保证数据一致性。

Database LUWS,可以显示或隐式Commit,Rollbace work;

SAP LUWS:

1. CALL FUNCTION ... IN UPDATE TASK,本地同步或异步function;

语法:

CALL FUNCTION update_function IN UPDATE TASK
[EXPORTING p1 = a1 p2 = a2 ...]
[TABLES t1 = itab1 t2 = itab2 ...].

2. CALL FUNCTION ... IN BACKGROUND TASK|UNIT,远程异步function;

3. PERFORM ... ON COMMIT,注册子程序在不同进程执行;

语法:

PERFORM subr ON { {COMMIT [LEVEL idx]} | ROLLBACK }.

控制SAP LUWS:每一个internal session都有一个SAP LUW;

1.COMMIT WORK

2.ROLLBACK WORK

3.SET UPDATE TASK LOCAL

语法:

SET UPDATE TASK LOCAL.

打开本地更新。

 

Database Locks:共享锁(Shared lock),允许创建其他共享锁,不能创建独占锁。独占锁(Exclusive lock),不允许创建其他锁。

这些OPEN SQL:SELECT SINGLE FOR UPDATE, INSERT, UPDATE, MODIFY, and DELETE创建独占锁。

隔离等级(isolation level):Uncommitted read (or dirty read)脏读,读取时不加共享锁,可能读取到未更新数据;Committed read,读取时加上共享锁,然后马上释放,保证读取到已更新数据;

所有数据库锁都将在下一次数据库提交或回滚之前释放。

 

SAP Locks:使用Tcode: SM12,可以查看SAP*锁表。

参考Locks function:ENQUEUE_EDEMOFLHT,DEQUEUE_EDEMOFLHT。

Function重要参数:

Parameter

Meaning

MODE_dbtab

锁类型, "S" :共享锁;"E":独占锁;

"X" :拓展独占锁,只可以请求一次;

"O":乐观锁,开始类似共享锁,但可以转换为独占锁;

key_fields

键值栏位,可以定义某个键对应值锁住,如果没有指定值,锁住所有行

_SCOPE

SAP LUW锁定时间. "1":处理同一程序锁; "2" :锁传递给更新程序;"3":处理同一程序锁或更新程序的锁;

 

Authorizations

          SAP权限检查。

权限Activity表:TACT;

用户定义Activity表:TACTZ;

维护权限字段Tcode:SU20;

维护权限对象Tcode:SU21;

生成权限对象及用户关系Tcode:PFCG;

显示权限对象及用户关系Tcode:SU03;

权限文件Tcode:SU02,PFCG;

用户主档Tcode:SU01;

权限检查:AUTHORITY-CHECK;

语法:

AUTHORITY-CHECK OBJECT auth_obj [FOR USER user]
                        ID id1 {FIELD val1}|DUMMY
                       [ID id2 {FIELD val2}|DUMMY]
                       [ID id10 {FIELD val10}|DUMMY].

上一篇:ABAP Help Document(15):8.数据类型转换


下一篇:如何验证fstab文件修改是否正确