Chinaunix首页 | 论坛 | 博客
  • 博客访问: 98495
  • 博文数量: 38
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 460
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-27 21:11
文章分类

全部博文(38)

我的朋友

分类: 信息化

2015-03-26 16:23:02


点击(此处)折叠或打开


  1. REPORT ZTHOMAS_DYNAMIC_SQL.
  2. PARAMETERS:SPFLI RADIOBUTTON GROUP GRP,
  3. SCARR RADIOBUTTON GROUP GRP,
  4. SAPLANE RADIOBUTTON GROUP GRP.
  5. *----------------------------------------------------------------------*
  6. * CLASS demo DEFINITION
  7. *----------------------------------------------------------------------*
  8. *
  9. *----------------------------------------------------------------------*
  10. CLASS DEMO DEFINITION.
  11. PUBLIC SECTION.
  12. CLASS-METHODS MAIN.
  13. ENDCLASS. "demo DEFINITION
  14. *----------------------------------------------------------------------*
  15. * CLASS demo IMPLEMENTATION
  16. *----------------------------------------------------------------------*
  17. *
  18. *----------------------------------------------------------------------*
  19. CLASS DEMO IMPLEMENTATION.
  20. METHOD MAIN.
  21. CONSTANTS LEFT TYPE TABNAME VALUE 'SFLIGHT'."constant variable declaration
  22. DATA:RIGHT TYPE TABNAME,
  23. STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR,
  24. TABLE_TYPE TYPE REF TO CL_ABAP_TABLEDESCR,
  25. COMP_TAB1 TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
  26. COMP_TAB2 TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
  27. COMP1 LIKE LINE OF COMP_TAB1,
  28. COMP2 LIKE LINE OF COMP_TAB2,
  29. SELECT TYPE TABLE OF EDPLINE,
  30. SUBLIST TYPE EDPLINE,
  31. FROM TYPE STRING,
  32. FIRST_ON TYPE ABAP_BOOL VALUE ABAP_TRUE,
  33. TREF TYPE REF TO DATA,
  34. ALV TYPE REF TO CL_SALV_TABLE.
  35. FIELD-SYMBOLS TYPE STANDARD TABLE.
  36. "根据选择的值对right赋值
  37. CASE 'X'.
  38. WHEN SPFLI.
  39. RIGHT = 'SPFLI'.
  40. WHEN SCARR.
  41. RIGHT = 'SCARR'.
  42. WHEN SAPLANE.
  43. RIGHT = 'SAPLANE'.
  44. ENDCASE.
  45. FIRST_ON = ABAP_TRUE.
  46. "初始化select sublist from
  47. CLEAR: SELECT, SUBLIST, FROM.
  48. READ CURRENT LINE LINE VALUE INTO RIGHT.
  49. "获取结构内容
  50. STRUCT_TYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( LEFT ).
  51. COMP_TAB1 = STRUCT_TYPE->GET_COMPONENTS( ).
  52. STRUCT_TYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( RIGHT ).
  53. COMP_TAB2 = STRUCT_TYPE->GET_COMPONENTS( ).
  54. "sql拼接
  55. FROM = LEFT && ` join ` && RIGHT && ` on ` .
  56. "循环两个结构,获取要查绚的字段目录
  57. LOOP AT COMP_TAB1 INTO COMP1 WHERE NAME IS NOT INITIAL.
  58. SUBLIST = LEFT && '~' && COMP1-NAME.
  59. APPEND SUBLIST TO SELECT.
  60. ENDLOOP.
  61. LOOP AT COMP_TAB2 INTO COMP2 WHERE NAME IS NOT INITIAL .
  62. READ TABLE COMP_TAB1 INTO COMP1
  63. WITH TABLE KEY NAME = COMP2-NAME.
  64. IF SY-SUBRC <> 0.
  65. "如果comp2的目录不存在于comp1中就加到comp1中
  66. APPEND COMP2 TO COMP_TAB1.
  67. SUBLIST = RIGHT && '~' && COMP2-NAME.
  68. APPEND SUBLIST TO SELECT.
  69. ELSE.
  70. "如果存在于comp1中就判断first_on的值如果为假,就进行sql拼接
  71. IF FIRST_ON = ABAP_FALSE.
  72. FROM = FROM && ` and `.
  73. ELSE.
  74. FIRST_ON = ABAP_FALSE.
  75. ENDIF.
  76. "sql拼接
  77. FROM = FROM && LEFT && `~` && COMP2-NAME &&
  78. ` = ` && RIGHT && `~` && COMP2-NAME.
  79. ENDIF.
  80. ENDLOOP.
  81. "动态创建内表
  82. STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( COMP_TAB1 ).
  83. TABLE_TYPE = CL_ABAP_TABLEDESCR=>CREATE( STRUCT_TYPE ).
  84. CREATE DATA TREF TYPE HANDLE TABLE_TYPE.
  85. ASSIGN TREF->* TO .
  86. "动态select语句
  87. SELECT (SELECT) INTO TABLE FROM (FROM).
  88. TRY .
  89. "调用salv进行数据展示
  90. CL_SALV_TABLE=>FACTORY(
  91. IMPORTING R_SALV_TABLE = ALV
  92. CHANGING T_TABLE =
  93. ).
  94. ALV->DISPLAY( ).
  95. CATCH CX_SALV_MSG.
  96. MESSAGE 'ALV display not possible' TYPE 'I' DISPLAY LIKE 'E'.
  97. ENDTRY.
  98. ENDMETHOD. "main
  99. ENDCLASS. "demo IMPLEMENTATION
  100. START-OF-SELECTION.
  101. DEMO=>MAIN( ).

阅读(605) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~