OPEN SQL简介

    OPEN SQL 语句包含有: SELECT,INSERT,UPDATE,MODIFY,DELETE,OPEN CURSOR, FETCH,CLOSE CURSOR,COMMIT WORK,ROLLBACK WORK等.

1.    SELECT语句

语法格式:
SELECT [INTO ] [FROM ] [WHERE ]
                 [GROUP BY ] [ORDER BY ]
其中: 指定要抓取的字段
        将读取的记录存放在work area中
        指定从那个TABLE中读取资料
        抓取资料的条件
        指定按那些字段分组
        排序的字段及方式
相关的系统变量:
       SY-SUBRC = 0    表示读取数据成功
                 <> 0 表示未找到符合条件的记录
       SY-DBLNT: 被处理过的记录的笔数.
相关的命令:
       EXIT. 退出循环.
       CHECK .如果逻辑表达式成立,则继续执行,否则,开
                              始下一次循环.
◆ .利用循环方式读取所有记录
SELECT ….ENDSELECT.是循环方式读取记录的.
       例如:
TABLES MARD.
SELECT [DISTINCT] * FROM MARD WHERE MATNR = ‘3520421700’.
      .
ENDSELECT.
(从MARD中抓取所有料号=3520421700的资料)
◆读取一笔资料
TABLES MARD.
SELECT SINGLE * FROM MARD WHERE MATNR = ‘3520421700’.
(从MARA中抓取一笔料号=3520421700的资料)
◆ 将读取的记录放在work area中,并且加入Internal table 中.
格式有:
     ... INTO
     ... INTO CORRESPONDING FIELDS OF
     ... INTO (f1, ..., fn) 变量组.
     ... INTO TABLE
     ... INTO CORRESPONDING FIELDS OF TABLE
     ... APPENDING TABLE
     ... APPENDING CORRESPONDING FIELDS OF TABLE
举例一:
TABLES MARD.
DATA:    BEGIN OF ITAB OCCURS 10,
                MATNR LIKE MARD-MATNR,
                WERKS LIKE MARD-WERKS,
                LGORT LIKE MARD-LGORT,
                LABST LIKE MARD-LABST,
          END OF ITAB.
SELECT MATNR WERKS LGORT LABST  
                INTO CORRESPONDING FIELDS OF ITAB
                FROM MARD
                WHERE MATNR = ‘3520421700’.
         APPEND ITAB.
         CLEAR ITAB.
ENDSELECT.
(将读取的结果放在Internal table ITAB中)
举例二.
TABLES MARD.
SELECT MATNR    MTART    MAKTX    INTO (t_matnr, t_mtart, maktx)
                 FROM MARD
                 WHERE MATNR = ‘3520421700’.
         .
ENDSELECT.
(从MARD中抓取料号=3520421700的料号、类型和描述,放在变量t_matnr, t_mtart, maktx中)。
◆按指定的字段排序
TABLES SBOOK.
SELECT * FROM SBOOK     WHERE CARRID = ‘LH’ AND
                                   CONNID = ‘0400’ AND
                                   FLDATE = ‘19950228’
              ORDER BY BOOKID [ASCENDING/DESCENDING].
     WRITE: / SBOOK-BOOKID,     SBOOK-CUSTOMID,
             SBOOK-CUSTTYPE,    SBOOK-SMOKER,
             SBOOK-LUGGWEIGHT, SBOOK-WUNIT,
             SBOOK-INVOICE.
ENDSELECT.
(利用参数ORDER BY所指定的字段排序)
◆ 抓取数据的条件叙述
(1) BETWEEN     AND   
例如:    WHERE YEAR BETWEEN 1995 AND 2000.
(2) LIKE
例如:    WHERE NAME LIKE ‘MIKE%’.
(‘%’是通配符号)
(3) IN ()
里面的任意一个值即可.
例如: WHERE PLANT IN (‘CHUNGLI’, ‘TAOYUAN’,’LIUTU’).
(表示PLANT 只要是’CHUNGLI’或’TAOYUAN’或’LIUTU’都可以).

2. INSERT 语句

◆ 从work area 加入到Internal Table中
格式: INSERT INTO VALUES
例如:
DATA:    BEGIN OF WA,
                CODE(6) TYPE C,
                NAME(30) TYPE C,
          END OF WA.
DATA:    VEN LIKE WA OCCURS 10.

WA-CODE    =    ‘530120’.
WA-NAME    =    ‘XINGDA ELECTRONICS CO.,LTD’.
INSERT INTO VEN VALUES WA .
如果work area的名称就是internal table的名称,可以直接写成:
        INSERT
例如:
DATA:    BEGIN OF WA OCCURS 10,
                CODE(6) TYPE C,
                NAME(30) TYPE C,
          END OF WA.

WA-CODE = ‘530120’.
WA-NAME    =    ‘XINGDA ELECTRONICS CO., LTD’.
INSERT WA.
◆ 从另外一个Internal table中INSERT 资料
格式:
INSERT FROM TABLE [ACCEPTING DUPLICATE KEY]
中非NULL的资料加入中,加上[ACCEPTING DUPLICATE KEY]能限制相同PRIMARY KEY不重复加入.

3. MODIFY 语法
MODIFY [FROM ].

4. DELETE 语法

       DELETE [FROM ].
      或: DELETE [WHERE ]

5. DATABASE CURSOR

     Database Cursor是一个数据库暂存区, 将经SELECT指令读取的记录存放至此暂存区, 再由此暂存区放至Work Area中, 可减少数据库读取的次数.
1.开启 Database Cursor
    语法:
          OPEN    CURSOR        FOR    SELECT …    WHERE
          Example:
                  TABLES    SPFLI.
                  DATA:    WA    LIKE    SPFLI,
                          C1    TYPE    CURSOR.
                  OPEN    CURSOR    C1 FOR SELECT    *    FROM    SPFLI
                         WHERE    AREA =’TAIWAN’.
    2.读取 Database Cursor的资料存入 Work Area
语法:
        FETCH    NEXT    CURSOR        INTO   
            Example:
                     FETCH    NEXT    CURSOR    C1    INTO    WA.
    读取下一笔Cursor位置的数据存入WA, 如果已无资料可读, SY-SUBRC <>0.
关闭 Database Cursor
语法:
        CLOSE    CURSOR   
        Example:
                 CLOSE    CURSOR    C1.

6.    COMMIT WORK & ROLLBACK WORK

要确定资料成功写入数据库,可使用COMMIT WORK指令,如:
     COMMIT    WORK.
相反的, 如果反悔要复原, 可使用 ROLLBACK    WORK, 可复原在上个COMMIT WORK指令之后的数据, 如:
     ROLLBACK    WORK.

你可能感兴趣的:(SAP,Database,sql,table,database,insert,delete,数据库)