OPEN SQL简介

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

1.    SELECT语句
语法格式:
SELECT <result> [INTO <target>] [FROM <source>] [WHERE <condition>]
                 [GROUP BY <fields>] [ORDER BY <sort order>]
其中: <result>指定要抓取的字段
        <target>将读取的记录存放在work area中
        <source>指定从那个TABLE中读取资料
        <condition>抓取资料的条件
        <fields>指定按那些字段分组
        <sort order>排序的字段及方式
相关的系统变量:
       SY-SUBRC = 0    表示读取数据成功
                 <> 0 表示未找到符合条件的记录
       SY-DBLNT: 被处理过的记录的笔数.
相关的命令:
       EXIT. 退出循环.
       CHECK <logistic statement>.如果逻辑表达式成立,则继续执行,否则,开
                              始下一次循环.
◆ .利用循环方式读取所有记录
SELECT ….ENDSELECT.是循环方式读取记录的.
       例如:
TABLES MARD.
SELECT [DISTINCT] * FROM MARD WHERE MATNR = ‘3520421700’.
      <Statements>.
ENDSELECT.
(从MARD中抓取所有料号=3520421700的资料)
◆读取一笔资料
TABLES MARD.
SELECT SINGLE * FROM MARD WHERE MATNR = ‘3520421700’.
(从MARA中抓取一笔料号=3520421700的资料)
◆ 将读取的记录放在work area中,并且加入Internal table 中.
格式有:
     ... INTO <work area>
     ... INTO CORRESPONDING FIELDS OF <work area>
     ... INTO (f1, ..., fn) 变量组.
     ... INTO TABLE <internal table>
     ... INTO CORRESPONDING FIELDS OF TABLE <internal table>
     ... APPENDING TABLE <internal table>
     ... APPENDING CORRESPONDING FIELDS OF TABLE <internal 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’.
         <Statements>.
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 <g1>    AND    <g2>
例如:    WHERE YEAR BETWEEN 1995 AND 2000.
(2) LIKE <g>
例如:    WHERE NAME LIKE ‘MIKE%’.
(‘%’是通配符号)
(3) IN (<g1>…<gn>)
是<g1>…<gn>里面的任意一个值即可.
例如: WHERE PLANT IN (‘CHUNGLI’, ‘TAOYUAN’,’LIUTU’).
(表示PLANT 只要是’CHUNGLI’或’TAOYUAN’或’LIUTU’都可以).

2. INSERT 语句
◆ 从work area 加入到Internal Table中
格式: INSERT INTO <database> VALUES <work area>
例如:
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 <internal table>
例如:
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 <itab1> FROM TABLE <itab2> [ACCEPTING DUPLICATE KEY]
将<itab2>中非NULL的资料加入<itab1>中,加上[ACCEPTING DUPLICATE KEY]能限制相同PRIMARY KEY不重复加入.

3. MODIFY 语法
MODIFY <internal table> [FROM <work area>].

4. DELETE 语法
       DELETE <internal table> [FROM <work area>].
      或: DELETE <internal table> [WHERE <conditions>]

5. DATABASE CURSOR
     Database Cursor是一个数据库暂存区, 将经SELECT指令读取的记录存放至此暂存区, 再由此暂存区放至Work Area中, 可减少数据库读取的次数.
1.开启 Database Cursor
    语法:
          OPEN    CURSOR    <c>    FOR    SELECT …    WHERE <condition>
          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    <c>    INTO    <wa>
            Example:
                     FETCH    NEXT    CURSOR    C1    INTO    WA.
    读取下一笔Cursor位置的数据存入WA, 如果已无资料可读, SY-SUBRC <>0.
关闭 Database Cursor
语法:
        CLOSE    CURSOR    <c>
        Example:
                 CLOSE    CURSOR    C1.

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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shin_zhong/archive/2009/09/18/4568358.aspx

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