zoukankan      html  css  js  c++  java
  • ABAP基础3:OPENSQL

    select result from source where condition group by fileds having condition order by fields into target

    select single cols  .. where  当存在多条符合条件的行项目时, single随机返回其中之一

    内表-当取得多条数据时, select结果会保存到内部定义的表中, 在abap里称为内表, internal table, ABAP内存中生成存储数据的虚拟表

    当into语句的结果不是保存到内表时, 而是字段或者结构体时(work area),最后要加上endselect语句, 相当于在loop循环执行select语句,每读取一条,追加到结构体中,直到结束

    select distinct cols ... where ...
    endselect.

    使用结构体保存查询数据

    data: gt_itab type standard table of sflight, "定义gt_itab类型与标准表sflight一致
    gs_wa type sflight. "定义gs_wa变量, 数据保存到这里
    select * into gs_wa from sflight where carrid eq 'AA'. "把查询结果全部保存到结构体中
    write: / gs_wa-carrid, gs_wa-connid.
    endselect.

    使用内表保存所有值

    data: gt_itab type standard table of sflight, "定义内表,数据保存到这里
    gs_wa type sflight.
    select * into table gt_itab from sflight where carrid eq 'AA'. "指定into到内表中,要加上关键字table
    
    loop at gt_itab into gs_wa.
    write: / gs_wa-carrid, gs_wa-connid.
    endloop.

     动态select语句

    data: gt_itab type standard table of sflight, "定义内表gt_itab
    gs_wa like line of gt_itab. "定义另一个变量,引用gt_itab内表的行,这不也是一张表么?
    
    data: gs_line(72) type c, "定义c类型变量,长度72
    gt_list like table of gs_line(72). "定义gt_list,引用gs_line,这不也是c类型么?
    
    gs_line = 'CARRID CONNID'. "将要查询的字段放到变量中
    
    select distinct (gs_line) into table gt_itab from sflight. "动态select条件要用括号括起来
    
    if sy-subrc eq 0.
      loop at gt_itab into gs_wa.
        write: / gs_wa-carrid, gs_wa-connid.
      endloop.
    endif.

    结构体

    当查询一条数据时使用结构体,使用星号能取得所有字段的值,此时使用corresponding fileds of语句会自动查找到相同字段名匹配赋值

    select ... into | corresponding fields of ..

    appending 与into

    内表使用appending,给内表追加数据, into是删除内表数据之后再插入数据

    data: gs_wa type sflight,
    gt_itab type table of sflight.
    
    select carrid connid 
    from spfli 
    into corresponding fields of table gt_itab
    package size 5.
    loop at gt_itab into gs_wa.
    write: / gs_wa-carrid,gs_wa-connid.
    endloop.
    endselect.

    from

    • client specified 解除自动client设置
    • bypassing buffer 绕过buffer,进行物理读
    • up to n rows 限制查询条目数,不就是limits么?

    选择动态表

    parameters p_tname type char10.  "定义gui上显示的输入框
    data gs_wa type sflight.
    select single * into gs_wa from (p_tname) where carrid = 'AA'. "点击执行按钮时,动态使用p_tname,赋值用户输入的sflight

    join

    默认是inner join

    查询航班名字

    types: begin of t_str,
    carrid type sflight-carrid,
    carrname type scarr-carrname,
    end of t_str.
    
    data: gs_str type t_str.
    select
    single a~carrid b~carrname into corresponding fields of gs_str from sflight as a inner join scarr as b on a~carrid eq b~carrid where a~carrid = 'AA'. write: gs_str-carrid, gs_str-carrname.

    动态条件

     就是给where字句传值

    FOR ALL ENTRIES

    类似嵌套select

    data gt_spfli type table of spfli.
    data gt_sflight type table of sflight.
    data gs_sflight type sflight.
    * table of tab 内表类型/ standar table of tab 标准表类型,也可以是其他sort表.堆表 / type tab仅仅是类型
    
    select * from spfli into table gt_spfli. "读取全部数据存到内表中,航班时刻表
    
    select * from sflight into table gt_sflight for all entries in gt_spfli " select ... for all enties in <tab> where <cond>
    where carrid = gt_spfli-carrid and connid = gt_spfli-connid.
    
    loop at gt_sflight into gs_sflight.
      write: / gs_sflight-carrid, gs_sflight-connid.
    endloop.

    聚合函数:AVG/COUNT/MAX/MIN/STDDEV/SUM

    取每个航班ID平均预约占用率

    data: gv_carrid type sflight-carrid,
    gv_connid type sflight-connid,
    gv_paymentsum type i.
    
    select carrid connid avg( paymentsum ) into (gv_carrid,gv_connid,gv_paymentsum) from sflight group by carrid connid.
    * 选择字段居然没有逗号隔开, 结构体变量,居然可以存储多个值, 什么时候用结构体变量,什么时候用内表? write:
    / gv_carrid, gv_connid, gv_paymentsum. endselect.

    排序语句order by

    order by primary key

    限制:

    • 只能根据表的key值排序
    • 只适用select *语句
    • 在join语句和视图中无法适用

    order by 字段 ascending,descending,默认升序

    data: gv_carrid type sflight-carrid,
    gv_connid type sflight-connid,
    gv_paymentsum type i.
    
    select carrid connid avg( paymentsum ) as paymentsum into (gv_carrid,gv_connid,gv_paymentsum) from sflight group by carrid connid
    order by paymentsum.  "聚合函数无法作为关键字进行排序,需要定义别名
    write: / gv_carrid, gv_connid, gv_paymentsum.
    endselect.

    Subquery,子查询,在select语句中嵌套select语句时使用

    Scalar Subquery

    • 返回一列值中的一个字段值(或者聚合函数执行结果值),类似join
    • 只能返回一个字段值
    • 以嵌套循环方式执行
    • 执行的次数是row行数目
    • 提升效率,当查询重复的编号及主数据表时使用
    data:gv_carrid type sflight-carrid,
    gv_connid type sflight-connid,
    gv_paymentsum type sflight-paymentsum.
    
    select single carrid connid paymentsum 
    into (gv_carrid, gv_connid, gv_paymentsum) "真坑,左括号后不能有空格,右括号前不能有空格
    from sflight as a
    where carrid in ( select carrid 
    from spfli 
    where carrid = a~carrid and 
    connid = a~connid ) "内层嵌套select括号要留空格
    and a~carrid = 'AA'.
    write: gv_carrid,gv_connid, gv_paymentsum.

    Non-scalar Subquery

    子查询存在数据时返回TRUE,不存在返回FALSE,使用exists实现

    data:gv_carrid type sflight-carrid,
    gv_connid type sflight-connid,
    gv_paymentsum type sflight-paymentsum.
    
    select single carrid connid paymentsum 
    into (gv_carrid, gv_connid, gv_paymentsum) "真坑,左括号后不能有空格,右括号前不能有空格
    from sflight as a
    where exists ( select *
    from spfli 
    where carrid = a~carrid and 
    connid = a~connid ) "内层嵌套select括号要留空格
    and a~carrid = 'AA'.
    write: gv_carrid,gv_connid, gv_paymentsum.

    INSERT

    • 插入一条数据
    insert into <target> values <wa>
    insert <target> from <wa>

    使用insert <dbtab>语句,需要先定义结构体 tables: <dbtab>

    往scarr插入air china数据

    data: gs_scarr type scarr.
    gs_scarr-carrid = 'CN'.
    gs_scarr-carrname = 'Air China'.
    gs_scarr-currcode = 'RMB'.
    gs_scarr-url = 'http://www.airchina.cn'.
    insert into scarr values gs_scarr."类似python字典 values后相当于字典名
    • 插入多条数据
    insert <target> from table <itab>
        [ACCEPTING DUPLICATE KEYS]  主键相同会报错,加上这句允许重复主键
    data: gt_spfli type table of spfli,
          gs_spfli type spfli.
          
    gs_spfli-carrid = 'CN'.
    gs_spfli-connid = '0001'.
    gs_spfli-cityfrom = 'beijing'.
    gs_spfli-cityto = 'Busan'.
    append gs_spfli to gt_spfli. "1.将值存在变量,变量追加内表内容
    
    gs_spfli-carrid = 'CN'.
    gs_spfli-connid = '0002'.
    gs_spfli-cityfrom = 'beijing'.
    gs_spfli-cityto = 'Busan'.
    append gs_spfli to gt_spfli.
    
    insert spfli from table gt_spfli accepting duplicate keys. "2.将内表插入物理表

    update

    update into <taget> <lines>.
    update<dbtab> [client specified] <lines>
    • 修改一条数据
    update <target> from <wa>.
    update <dbtab> 要先定义tables: <dbtab> 结构体
    • 修改多条数据

    一次性将内表的多条数据反映到数据库表中

    update <target> from table <itab>

    修改指定字段, 比直接指定工作区的方法性能高

    update <target> set <set1> <set2> where <cond>  set1字段1,set2字段2
    data gs_spfli type spfli.
    
    move 'CN' to gs_spfli-carrid.
    move '0001' to gs_spfli-connid.
    move 'Shanghai' to gs_spfli-cityfrom.
    gs_spfli-cityto = 'guangzhou'.
    update spfli from gs_spfli. "内表反映更新数据表
    
    update spfli
    set cityto = 'beging'
    where carrid = 'CN'
    and connid = '0001'.
    if sy-subrc = 0.
    write 'update is successful'."set字段更新到数据表
    endif.

    delete

    • 删除一条数据
    delete <target> from <wa>.
    delete <dbtab>  要先定义tables: <dbtab>
    • 删除多条数据
    delete from <target> where <cond>.
    data gs_spfli type spfli.
    move 'CN' to gs_spfli-carrid.
    move '0001' to gs_spfli-connid.
    
    delete spfli from gs_spfli. "删除一条数据
    
    delete from spfli where carrid = 'CN'. "删除符合where子句的数据
    
    if sy-subrc = 0.
      write 'delete successful'.
    endif.

    modify

    1. 以主键为基准更新数据
    2. 存在数据,则用update更新
    3. 不存在数据,则用insert插入
    • 插入一条数据
    modify <target> from <wa>.
    modify <dbtab> 要先定义数据类型tables: <dbtab>
    • 插入多条数据,一次性修改或者追加内表所有数据
    modify <target> from table <itab>
    data:
    gt_spfli type table of spfli, "定义内表
    gs_spfli type spfli. "定义表数据类型
    
    gs_spfli-carrid = 'CN'.
    gs_spfli-connid = '0001'.
    gs_spfli-cityfrom = 'beijing'.
    modify spfli from gs_spfli. "从普通数据类型修改数据到表
    
    gs_spfli-carrid = 'CN'.
    gs_spfli-connid = '0001'.
    gs_spfli-cityfrom = 'shanghai'.
    append gs_spfli to gt_spfli. "追加数据到内表  主键是carrid connid,这条数据会覆盖上面那一条
    
    gs_spfli-carrid = 'CN'.
    gs_spfli-connid = '0003'.
    gs_spfli-cityfrom = 'shanghai'.
    append gs_spfli to gt_spfli. "继续追加数据到内表 表中没有这条数据,直接插入到表
    
    modify spfli from table gt_spfli.
    • 在GUI上执行SQL命令
    parameters: p_sql(255) type c.
    
    data: go_sql type ref to cl_sql_statement. "cl_sql_statement是一个类
    data: gv_sql type string.
    data: gd_ret type i.
    
    create object go_sql. "这是实例化一个对象?
    gv_sql = p_sql. "GUI屏幕输入实参
    
    translate gv_sql to upper case. "将GUI屏幕输入内容转换成大写
    
    call method go_sql->execute_update "execute_update 是一个类方法,对象调用类方法->?  这个符号前后不能有空格
     exporting
      statement = gv_sql "给类方法传值?输入内容?值传给谁?
     receiving
      rows_processed = gd_ret."输出结果?到哪里去?

    输入: UPDATE SFLIGHT SET CONNID = '10' WHERE CARRID = 'AA'

  • 相关阅读:
    Qt-char类型的字符串转换成数字和数字转换成char类型字符串
    Qt-label显示的汉字自动换行
    Qt-Libmodbus
    Linux-uboot命令之EXT格式文件系统操作命令
    Linux-uboot命令之FAT格式文件系统操作命令
    Linux-使用uboot命令将Linux镜像和设备树文件下载到EMMC中
    Linux-在uboot中更新uboot(包含SD卡和EMMC)
    Linux-使用uboot命令将Linux镜像和设备树文件下载到DRAM中
    Qt-QCustomPlot(画坐标系统)的简单操作
    Qt-QTableView的简单使用
  • 原文地址:https://www.cnblogs.com/jenvid/p/8288449.html
Copyright © 2011-2022 走看看