SAP ABAP基础知识自学笔记二

三、各种定义

基本格式

DATA:XX TYPE YY VALUE'ZZ'

各种来了

*参照数据字典定义

*参照数据元素定义

data v9 type zdata_jg_20100325_01.

*参照数据库表的字段定义

data v10 type scarr-carrid.

*定义常量

CONSTANTS c1 TYPE i VALUE '1'.

*定义单值选侧屏幕

PARAMETERS p1(8) TYPE n default 'AA'.

*参照自定义类型定义变量

TYPES t1(10) TYPE c.

DATA v9 TYPE t1.

*参照自定义变量定义变量

DATA v10 LIKE v9.

*参照数据字典中的TYPE GROUP定义数据类型

TYPE-POOLS zjg01 .

data v20 type zjg01_z1.

定义结构

DATA : BEGIN OF wa,

       carrid TYPE spfli-carrid,

       connid TYPE spfli-connid,

       cityfrom TYPE spfli-cityfrom,

       cityto TYPE spfli-cityto,

       distance TYPE spfli-distance,

       distid TYPE string,

       END OF wa.

根据结构定义内表

DATA itab LIKE TABLE OF wa.

流程控制-IF…ELSE

IF a > 12.

 WRITE : '他比12大'.

ELSE.

 WRITE : '他比12小'.

ENDIF.

循环

DO 10 TIMES.

 WRITE: / '这是第',ie,'次'.

 ie = ie + 1.

ENDDO.

把内表循环到结构中

LOOP AT itab INTO wa.

 WRITE : / wa-id.

ENDLOOP.

内表各种操作-collect

在内表中一定有一个数值型的字段,而且其他字段均为字符型

把数值型字段上的数加起来

DATA : BEGIN OF wa,

      a(4) TYPE c,

      b(4) TYPE c,

      c TYPE i,

      END OF wa.

DATA itab LIKE TABLE OF wa.

wa-a = '001'.

wa-b = '0101'.

wa-c = 100.

*APPEND wa TO itab.

COLLECT wa INTO itab.

wa-a = '002'.

wa-b = '0201'.

wa-c = 200.

*APPEND wa TO itab.

COLLECT wa INTO itab.

wa-a = '002'.

wa-b = '0201'.

wa-c = 100.

*APPEND wa TO itab.

COLLECT wa INTO itab.

LOOP AT itab INTO wa.

 WRITE : / wa-a,

            wa-b,

            wa-c.

ENDLOOP.

内表各种操作-read

LOOP AT itab1 INTO wa.

 READ TABLE itab2 INTO wa1 WITH KEY carrid = wa-carrid.

 IF sy-subrc <> 0.

 clear wa1.

    move-corresponding wa to wa1.

    append wa1 to itab2.

 ENDIF.

ENDLOOP.

根据结构修改内表

modify itab from wa.

SQL操作

1、把SQL结果放入内表中

SELECT carrid connid cityfrom cityto distance distid

FROM spfli

INTO CORRESPONDING FIELDS OF TABLE itab. 放到了内表中

INTO CORRESPONDING FIELDS OF wa.放到了结构中

2.1、多表查询

SELECT scarr~carrid scarr~carrname spfli~connid spfli~cityfrom

spfli~cityto

  FROM scarr INNER JOIN spfli

      ON scarr~carrid = spfli~carrid

   INTO CORRESPONDING FIELDS OF TABLE itab.

2.2、更多表查询

SELECT spfli~carrid spfli~connid spfli~cityfrom spfli~cityto

sbook~fldate sbook~bookid sbook~customid sbook~custtype scustom~name

scustom~street

   FROM spfli INNER JOIN sbook

      ON spfli~carrid = sbook~carrid AND spfli~connid = sbook~connid

          INNER JOIN scustom

             ON sbook~customid = scustom~id

                INTO CORRESPONDING FIELDS OF TABLE itab.

各种关键字

APPEND wa TO itab.把结构搞到内表中

MOVE TO 结构赋值

modify itab from wa根据结构修改内表

MOVE-CORRESPONDING str1 TO str3.同名字段赋值

 

分支研究

data:v1 type i value '1'.

v2 type i.

do 50 times.

v2 = v1 ** 2.

write : v1,v2.

v1=v1 +1.

enddo.

函数来了

字符串操作

CONCATENATE wa-fldate wa-week INTO wa-www SEPARATED BY

'/星期'.

子程序

三种类型:值传递,值传递并返回

# 1.值传递-实参在传递过程中不发生改变,只是传递一个实参的拷贝,适用于值不能改变的单值,不适合内表参数传递。

# 2.值传递并返回结果-传递一个实参的拷贝,实参的拷贝发生改变,返回给实参,适合于可以改变的单值传递,不适合内表参数传递。

# 3.引用传递-传递实参,子程序运行结束后,实参根据子程序运行结果,适合单值也适合内表。

看看代码就知道怎么用了

DATA : a TYPE i,

       b TYPE i,

       c TYPE i.

PERFORM x  USING a.这是值传递

PERFORM y CHANGING b.这是值传递改变

PERFORM z CHANGING c.这是引用传递

PERFORM xyz USING a

            CHANGING b

                     c.连起来就是这么用滴

*值传递和值传递引用改变需要写个value,引用传递不需要

FORM xyz USING value(f1) TYPE i值传递

         CHANGING value(f2) TYPE i值传递改变

                  f3 TYPE i.引用传递

ENDFORM.

*如果传入的是内表,子程序中可以这么写

FORM xyz CHANGING p-itab like itab.

*也可以这么写

FORM xyz CHANGING p-itab type itab.

 

屏幕

START-OF-SELECTION.    "主程序开始了!

 WRITE : / text-001,sy-lsind.

AT LINE-SELECTION.

 CASE sy-lsind.

    WHEN '1'.

      WRITE : / '第一级详细列表',sy-lsind.

    WHEN '2'.

      WRITE : / '第二级详细列表',sy-lsind.

    WHEN '3'.

      WRITE : / '第三级详细列表',sy-lsind.

 ENDCASE.

 

单值选择屏幕

PARAMETER p1 TYPE scarr-carrid DEFAULT 'AA'.

多重选择屏幕

TABLES scarr.相当于data scarr type scarr.

SELECT-OPTIONS pi FOR scarr-carrid. for后面不是类型是变量

*下面是用法

SELECT carrid carrname FROM scarr

 INTO CORRESPONDING FIELDS OF TABLE itab

 WHERE carrid IN pi.

LOOP AT itab INTO wa.

 WRITE : / wa-carrid,

            wa-carrname.

ENDLOOP.

 

 

 

data : v1 type i value '1'.

data : v2 type i value '1'.

data : v3 type i value '2'.

data : v4 type i value '3'.

do v1 times.

write : v1, '-' , v2.

write : v1, '-' , v2.

write : v1, '-' , v2.

 

 

 

 

data : begin of wa1,

        ebeln(3) type c,

        dat type d,

        menge type i,

       end of wa1.

 data itab like table of wa1.

 wa1-ebeln = '001'.

 wa1-dat = '20070803'.

 wa1-menge = '10'.

 append wa1 to itab.

 clear wa1.

 

 wa1-ebeln = '001'.

 wa1-dat = '20070903'.

 wa1-menge = '20'.

 append wa1 to itab.

 clear wa1.

 

 wa1-ebeln = '002'.

 wa1-dat = '20080303'.

 wa1-menge = '20'.

 append wa1 to itab.

 clear wa1.

 

 wa1-ebeln = '003'.

 wa1-dat = '20080603'.

 wa1-menge = '30'.

 append wa1 to itab.

 clear wa1.

 

loop at itab into wa1.

write : / wa1-ebeln,

          wa1-dat,

          wa1-menge.

 

endloop.

 

 

uline.

data : begin of wa2,

        ebeln(3) type c,

        menge type i,

        end of wa2.

   data itab2 like table of wa2.

loop at itab into wa1.

move-corresponding wa1 to wa2.

collect wa2 into itab2.

endloop.

 

loop at itab2 into wa2.

write : / wa2-ebeln,

          wa2-menge.

endloop.

 

 

 

sy-subrc    判断有没有东西

 

condense 字符型    //去空格

 

 

for all entries in 内表 

 

 

DATA wa LIKE LINE OF itab.   //参照内表建结构

 

sort it by carrid connid fldate.

delete adjacent duplicates from it comparing all fields.

 

转载于:https://www.cnblogs.com/GavinCome/archive/2010/11/23/1885039.html

你可能感兴趣的:(SAP ABAP基础知识自学笔记二)