SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Show results of queries in ALV



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Tue Nov 18, 2008 1:27 am    Post subject: Show results of queries in ALV Reply with quote

Code:
*======================================================================*
* PROGRAM....: ZZSQL_QUERY
*----------------------------------------------------------------------*
* AUTHOR.....: Luciano Rebuffi.
*----------------------------------------------------------------------*
* OBJETIVO...: SHOW RESULTS OF QUERIES IN ALV                          *
*              USE:                                                    *
*              - SELECT EDITOR: INSERT THE FIELDS TO GET. IN CASE OF   *
*                  JOIN YOU MUST SPECIFY THE TABLE(EX: MKPF~MBLNR)     *
*              - FROM Editor : INSERT THE TABLES TO QUERY              *
*              - WHERE Editor: INERT THE CONDITION. *
*                 IN CASE OF   *
*                  JOIN YOU MUST SPECIFY THE TABLE(EX: MKPF~MBLNR)     *

*----------------------------------------------------------------------*
* PARAMETROS.: ROWS: NUMBER OF REGISTERS TO GET.                       *
*              DOWN: DOWNLOAD THE CODE TO THE LOCAL PC. (C:\SQL.txt)   *
*              CONV: USE EXIT CONVERSION FOR FIELDS                    *
*              TEC: USE TECHNICAL NAMES OF FIELDS             *
*----------------------------------------------------------------------*

REPORT  zzsql_query.

TYPE-POOLS:
  icon,
  rsfs,
  rsds,
  slis.

TABLES:
  sscrfields.       "Campos en las imágenes de selección

DATA:
  g_repid   LIKE sy-repid.

DATA:
      delim     TYPE c.

DATA: code      TYPE TABLE OF rssource-line,
      prog(8)   TYPE c,
      msg(120)  TYPE c,
      lin(3)    TYPE c,
      wrd(10)   TYPE c,
      off(3)    TYPE c.

DATA:
  first    TYPE i,
  l_join   TYPE c,
  l_table  LIKE rsrd1-tbma_val,
  l_single TYPE c.

DATA: onelinecode LIKE LINE OF code.

DATA : fcat TYPE slis_t_fieldcat_alv.
DATA : wcat LIKE LINE OF fcat.

CONSTANTS: c_line_length TYPE i VALUE 80.

* define table type for data exchange
TYPES: BEGIN OF mytable_line,
         line(c_line_length) TYPE c,
       END OF mytable_line.

TYPES: ty_table TYPE TABLE OF mytable_line.

DATA : BEGIN OF tblcol_tab OCCURS 0,
           tbl TYPE string,
           col TYPE string.
DATA : END OF tblcol_tab.

DATA: BEGIN OF ti_dd03l OCCURS 0,
         tabname   LIKE dd03l-tabname,
         fieldname LIKE dd03l-fieldname,
         position  LIKE dd03l-position,
      END OF ti_dd03l.

DATA: BEGIN OF ti_param OCCURS 0,
         tabname(c_line_length) TYPE c,
         fieldname(c_line_length) TYPE c,
         param(c_line_length) TYPE c,
         tipo TYPE c,
      END OF ti_param.

* Tablas con los datos de la consulta.
DATA:
  ti_select TYPE ty_table,
  ti_from   TYPE ty_table,
  ti_where  TYPE ty_table,
  ti_tmp    TYPE ty_table.

DATA:
  myline LIKE LINE OF ti_select,
  myline1 LIKE LINE OF ti_select.

DATA:
  functxt  TYPE smp_dyntxt.

RANGES: r_cond FOR myline-line.

*----------------------------------------------------------------------*
* PARAMETROS
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK bl01 WITH FRAME TITLE tit .

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON 10(45) sel USER-COMMAND sel
                                        VISIBLE LENGTH 12.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON 10(45) frm USER-COMMAND frm
                                        VISIBLE LENGTH 12.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON 10(45) whr USER-COMMAND whr
                                        VISIBLE LENGTH 12.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN END   OF BLOCK bl01.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(15) text1,
                  POSITION 20.
PARAMETER: rows   TYPE i.        "Up To N rows
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(15) text2,
                  POSITION 20.
PARAMETER: down   AS CHECKBOX.   "Download
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(16) text3,
                  POSITION 20.
PARAMETER: conv   AS CHECKBOX.   "Conversión
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(16) text4,
                  POSITION 20.
PARAMETER: tec   AS CHECKBOX.   "Nombre técnico
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN BEGIN OF BLOCK bl02 WITH FRAME TITLE tit1 .

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(16) text5,
                  POSITION 20.
PARAMETER: vis   RADIOBUTTON GROUP rad    "Visualizar ALV
                 DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(16) text6,
                  POSITION 20.
PARAMETER: gen   RADIOBUTTON GROUP rad.   "Generar reporte

SELECTION-SCREEN: COMMENT 30(10) text7,
                  POSITION 41.
PARAMETER: repid  LIKE sy-repid.          "Nombre del reporte

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN END   OF BLOCK bl02.

* Botón de Borrar todo en el STATUS GUI.
SELECTION-SCREEN: FUNCTION KEY 1.

INITIALIZATION.

  tit   = 'Parámetros:'.
  tit1  = 'Opciones:'.
  text1 = 'Up To N rows'.
  text2 = 'Download code'.
  text3 = 'Sin conv. x dom.'.
  text4 = 'Nombre técnico'.
  text5 = 'Visualizar ALV'.
  text6 = 'Generar reporte'.
  text7 = 'Reporte'.

* Agrego el boton de borrar todo en STATUS GUI.
  functxt-icon_id   = icon_delete.
  functxt-quickinfo = 'Borrar todo'.
  sscrfields-functxt_01 = functxt.

* Creo la visualización del botón de SELECT en la DYNPRO.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_select_detail
      text   = 'SELECT'
      info   = 'Visualizar SELECT'
    IMPORTING
      RESULT = sel
    EXCEPTIONS
      OTHERS = 0.

* Creo la visualización del botón de FROM en la DYNPRO.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_table_settings
      text   = 'FROM'
      info   = 'Visualizar FROM'
    IMPORTING
      RESULT = frm
    EXCEPTIONS
      OTHERS = 0.

* creo la visualización del botón de WHERE en la dynpro.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_filter
      text   = 'WHERE'
      info   = 'Visualizar WHERE'
    IMPORTING
      RESULT = whr
    EXCEPTIONS
      OTHERS = 0.

  IMPORT ti_select FROM MEMORY ID 'ZSELECT'.
  IMPORT ti_from   FROM MEMORY ID 'ZFROM'.
  IMPORT ti_where  FROM MEMORY ID 'ZWHERE'.

  tec = 'X'.

  PERFORM generar_repid.

*-----------------------------------------------------------------------
* AT SELECTION-SCREEN
*-----------------------------------------------------------------------

AT SELECTION-SCREEN.

  CASE sy-ucomm.
    WHEN 'FC01'.
      REFRESH: ti_select, ti_where, ti_from.
      FREE MEMORY ID: 'ZSELECT', 'ZFROM', 'ZWHERE'.
    WHEN 'SEL'.
      PERFORM editor_table  USING ' '
                                  'SELECT'
                         CHANGING ti_select[].
    WHEN 'FRM'.
      PERFORM editor_table  USING ' '
                                  'FROM'
                         CHANGING ti_from[].
    WHEN 'WHR'.
      PERFORM editor_table  USING ' '
                                  'WHERE'
                         CHANGING ti_where[].
    WHEN OTHERS.
* Do Nothing !!.
  ENDCASE.

AT SELECTION-SCREEN ON RADIOBUTTON GROUP rad.

  PERFORM set_screen.
  PERFORM generar_repid.

AT SELECTION-SCREEN OUTPUT.

  PERFORM set_screen.
  PERFORM generar_repid.

START-OF-SELECTION.

  PERFORM validar_repid.

  PERFORM f_runsql.


************************************************************************
*  F O R M S
************************************************************************

*&---------------------------------------------------------------------*
*&      Form  F_RUNSQL
*&---------------------------------------------------------------------*
FORM f_runsql.

  DATA:
    numcols   TYPE i,
    aggfun    TYPE i,
    pos       TYPE i.

  DATA:
    l_tfill TYPE sy-tfill,
    l_todos TYPE c,
    l_tabix LIKE sy-tabix,
    l_carac TYPE c,
    l_cond  TYPE sy-tabix,
    l_param TYPE sy-tabix.

  DATA : BEGIN OF ti_val OCCURS 0,
             val(80),
         END OF ti_val.

  DATA: lt_wh     TYPE ty_table WITH HEADER LINE,
        lt_where  TYPE ty_table.

  FIELD-SYMBOLS: <fs> TYPE mytable_line.

  CHECK ti_select[] IS NOT INITIAL AND ti_from[] IS NOT INITIAL.

  EXPORT ti_select TO MEMORY ID 'ZSELECT'.
  EXPORT ti_from   TO MEMORY ID 'ZFROM'.
  EXPORT ti_where  TO MEMORY ID 'ZWHERE'.

  CLEAR:  code, l_join, l_table, l_todos.
  REFRESH code.

  MOVE 0 TO : first, numcols, aggfun.

* #####################
* ####### FROM ########
* #####################

* Verifico si se realiza un JOIN.
  SEARCH ti_from FOR 'JOIN'.

  IF sy-subrc EQ 0.
    l_join = 'X'.
    CLEAR l_table.
  ENDIF.

  IF l_join IS INITIAL.
* Busco el nombre de la tabla.
    SEARCH ti_from FOR 'FROM'.

    IF sy-subrc EQ 0.

      l_tabix = sy-tabix.

      DO.
        READ TABLE ti_from INTO myline INDEX l_tabix.
        IF sy-subrc NE 0.
          EXIT.
        ENDIF.
        TRANSLATE myline TO UPPER CASE.
        CONDENSE myline.
        REFRESH ti_val.
        SPLIT myline AT space INTO TABLE ti_val.

        LOOP AT ti_val WHERE val NE 'FROM'.
          l_table = ti_val-val.
          EXIT.
        ENDLOOP.

        IF sy-subrc EQ 0.
          EXIT.
        ELSE.
          ADD 1 TO l_tabix.
        ENDIF.
      ENDDO.
    ELSE.
      CALL FUNCTION 'POPUP_TO_INFORM'
        EXPORTING
          titel = g_repid
          txt2  = 'FROM DMLs Only Please!'
          txt1  = 'Correct & Retry'.

      STOP.
    ENDIF.
  ENDIF.

* #####################
* ###### SELECT #######
* #####################
  ti_tmp[] = ti_select[].

* Busco el select.
  SEARCH ti_tmp FOR 'SELECT'.

  IF sy-subrc NE 0.
* Falta el SELECT.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = g_repid
        txt2  = 'SELECT DMLs Only Please!'
        txt1  = 'Correct & Retry'.

    STOP.
  ELSE.
    READ TABLE ti_tmp INTO myline INDEX sy-tabix.
    TRANSLATE myline TO UPPER CASE.
    REPLACE 'SELECT' IN myline WITH space.
    CONDENSE myline.
    MODIFY ti_tmp FROM myline INDEX sy-tabix.
  ENDIF.

  SEARCH ti_tmp FOR 'SINGLE'.

  IF sy-subrc EQ 0.
* Se trata de un SELECT SINGLE.
    l_single = 'X'.
    READ TABLE ti_tmp INTO myline INDEX sy-tabix.
    TRANSLATE myline TO UPPER CASE.
    REPLACE 'SINGLE' IN myline WITH space.
    CONDENSE myline.
    MODIFY ti_tmp FROM myline INDEX sy-tabix.
  ELSE.
    CLEAR l_single.
  ENDIF.

  IF l_join IS INITIAL.
    LOOP AT ti_tmp INTO myline.
      CONDENSE myline.
      off = STRLEN( myline ).
      IF off = 0.
        CONTINUE.
      ELSE.
* Verifico si se requieren todos los campos de la tabla.
        DO.
          l_carac = myline+pos(1).
          IF l_carac = '*'.
* Tengo que buscar todos los campos de la tabla para poder
* visualizarlos.
            PERFORM buscar_campos TABLES ti_dd03l
                                  USING  l_table.
            l_todos = 'X'.
            EXIT.
          ELSE.
            IF pos < off.
              ADD 1 TO pos.
            ELSE.
              EXIT.
            ENDIF.
          ENDIF.
        ENDDO.
      ENDIF.
    ENDLOOP.
  ENDIF.

  IF l_todos IS INITIAL.
    LOOP AT ti_tmp INTO myline.
      IF l_join EQ 'X'.
*      '~'
        TRANSLATE myline TO UPPER CASE.
        CONDENSE myline.
        REFRESH ti_val.
        SPLIT myline AT space INTO TABLE ti_val.
        LOOP AT ti_val WHERE val NE space.
          SPLIT ti_val-val AT '~' INTO tblcol_tab-tbl tblcol_tab-col.
          APPEND tblcol_tab.
        ENDLOOP.
      ELSE.
* Se seleccionaron varios campos.
        TRANSLATE myline TO UPPER CASE.
        CONDENSE myline.
        REFRESH ti_val.
        SPLIT myline AT space INTO TABLE ti_val.

        LOOP AT ti_val WHERE val NE space.
          tblcol_tab-tbl = l_table.
          tblcol_tab-col = ti_val-val.
          APPEND tblcol_tab.
        ENDLOOP.
      ENDIF.
    ENDLOOP.
  ELSE.
* Cargo los campos del '*' (Asterisco).

    DESCRIBE TABLE ti_dd03l LINES l_tfill.

*    IF l_tfill = 99.
    LOOP AT ti_select ASSIGNING <fs>.
* Elimino el '*' (Asterisco), ya que solo se pueden visualizar
* 99 campos en un ALV.
      TRANSLATE <fs>-line USING '* '.
    ENDLOOP.
*    ENDIF.

    REFRESH tblcol_tab.
    LOOP AT ti_dd03l.
      tblcol_tab-tbl = ti_dd03l-tabname.
      tblcol_tab-col = ti_dd03l-fieldname.
      APPEND tblcol_tab.

*      CHECK l_tfill = 99.
* Cargo los campos, ya que saque el '*' (Asterisco).
      APPEND ti_dd03l-fieldname TO ti_select.
    ENDLOOP.
  ENDIF.

  IF tblcol_tab[] IS INITIAL.
* No hay ningún campo seleccionado.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = g_repid
        txt2  = 'Table~Column Open SQL is MUST'
        txt1  = 'No Fields'.
    STOP.
  ENDIF.


* #####################
* ###### WHERE  #######
* #####################

  LOOP AT ti_where INTO myline.
    TRANSLATE myline TO UPPER CASE.
    SPLIT myline AT space INTO TABLE lt_wh.
    APPEND LINES OF lt_wh TO lt_where.
  ENDLOOP.

  DELETE lt_where WHERE LINE IS INITIAL.

* Operadores.
  PERFORM cargo_operadores.

  LOOP AT lt_where INTO myline
                   WHERE LINE IN r_cond.

    l_cond = sy-tabix.
    l_param = sy-tabix + 1.
    READ TABLE lt_where INTO myline1 INDEX l_param.

    CHECK sy-subrc IS INITIAL.

    IF myline1(1) EQ '''' OR myline1 EQ 'SPACE' OR myline1 EQ 'INITIAL' OR
       myline1(3) EQ 'SY-' OR myline1(5) EQ 'SYST-'.
      CONTINUE.
    ENDIF.

    IF l_join IS INITIAL.
* SELECT comun.
      MOVE:
        myline1 TO ti_param-param,
        l_table TO ti_param-tabname.

      DO 2 TIMES.

        l_cond = l_cond - 1.

        READ TABLE lt_where INTO myline1 INDEX l_cond.

        CHECK sy-subrc IS INITIAL AND myline1 NE 'NOT'.
* Obtengo el nombre del campo .
        MOVE myline1 TO ti_param-fieldname.
        EXIT.
      ENDDO.

      IF myline EQ 'IN'.
* Select-option.
        MOVE: 'S' TO ti_param-tipo.
      ELSE.
* Parameter.
        MOVE: 'P' TO ti_param-tipo.
      ENDIF.
      APPEND ti_param.

    ELSE.
* Se realizó un JOIN.

      MOVE myline1 TO ti_param-param.

      DO 2 TIMES.

        l_cond = l_cond - 1.

        READ TABLE lt_where INTO myline1 INDEX l_cond.

        CHECK sy-subrc IS INITIAL AND myline1 NE 'NOT'.
* Obtengo el nombre  de la tabla y del campo.
        SPLIT myline1 AT '~' INTO ti_param-tabname ti_param-fieldname.
        EXIT.
      ENDDO.

      IF myline EQ 'IN'.
* Select-option.
        MOVE: 'S' TO ti_param-tipo.
      ELSE.
* Parameter.
        MOVE: 'P' TO ti_param-tipo.
      ENDIF.
      APPEND ti_param.

    ENDIF.
  ENDLOOP.

* ###################################
* ###### Genero el REPORT ###########
* ###################################

  PERFORM generar_cf.

  IF down IS NOT INITIAL.
* Download del código fuente.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename = 'C:\SQL.txt'
      TABLES
        data_tab = code.
  ENDIF.

  IF vis IS NOT INITIAL.
* Genero una subrutina.
    PERFORM generar_subrutina.
  ELSE.
* Genero un reporte de modo temporal.
    PERFORM generar_reporte.
  ENDIF.

ENDFORM.        "F_RUNSQL

*&---------------------------------------------------------------------*
*&      Form  buscar_campos
*&---------------------------------------------------------------------*
*       Busco todos los campos de la tabla seleccionada.
*----------------------------------------------------------------------*
*      -->PT_DD03L  Tabla con los campos.
*      -->P_TABLE   Tabla.
*----------------------------------------------------------------------*
FORM buscar_campos  TABLES   pt_dd03l STRUCTURE ti_dd03l
                    USING    p_table.

  SELECT tabname fieldname position
    INTO CORRESPONDING FIELDS OF TABLE pt_dd03l
    FROM dd03l
    UP TO 99 ROWS
    WHERE tabname       EQ p_table
      AND NOT fieldname LIKE '.INCLU%'
      AND datatype      NE 'CLNT'.

  SORT pt_dd03l BY position.

ENDFORM.                    " buscar_campos

*&---------------------------------------------------------------------*
*&      Form  editor_table
*&---------------------------------------------------------------------*
*       Editor de texto.
*----------------------------------------------------------------------*
*      -->PT_LINES  Líneas del texto.
*      -->P_DISPLAY Visualizar o editar.
*----------------------------------------------------------------------*
FORM editor_table  USING    p_display TYPE s38e-app_disp
                            p_name    TYPE trdir-name
*                   CHANGING pt_lines  TYPE mytable_line.
                    CHANGING pt_lines  TYPE ty_table.

  DATA:
    l_changed LIKE s38e-buf_varied,
    l_subrc   LIKE sy-subrc.

  CALL FUNCTION 'EDITOR_TABLE'
    EXPORTING
      display = p_display
      name    = p_name
    IMPORTING
      changed = l_changed
      subrc   = l_subrc
    TABLES
      content = pt_lines.

ENDFORM.                    " editor_table

*&---------------------------------------------------------------------*
*&      Form  cargo_operadores
*&---------------------------------------------------------------------*
*       Cargo los operadores utilizados en el WHERE del SELECT.
*----------------------------------------------------------------------*
FORM cargo_operadores .

  r_cond-sign = 'I'.
  r_cond-option = 'EQ'.
*  =, EQ
  r_cond-low    = 'EQ'.
  APPEND r_cond.
  r_cond-low    = '='.
  APPEND r_cond.
*  <>, NE
  r_cond-low    = 'NE'.
  APPEND r_cond.
  r_cond-low    = '<>'.
  APPEND r_cond.
*  <, LT
  r_cond-low    = 'LT'.
  APPEND r_cond.
  r_cond-low    = '<'.
  APPEND r_cond.
*  >, GT
  r_cond-low    = 'GT'.
  APPEND r_cond.
  r_cond-low    = '>'.
  APPEND r_cond.
*  <=, LE
  r_cond-low    = 'LE'.
  APPEND r_cond.
  r_cond-low    = '<='.
  APPEND r_cond.
*  >=, GE
  r_cond-low    = 'GE'.
  APPEND r_cond.
  r_cond-low    = '>='.
  APPEND r_cond.
*  BETWEEN
  r_cond-low    = 'BETWEEN'.
  APPEND r_cond.
*  LIKE
  r_cond-low    = 'LIKE'.
  APPEND r_cond.
*  IN
  r_cond-low    = 'IN'.
  APPEND r_cond.

ENDFORM.                    " cargo_operadores

*&---------------------------------------------------------------------*
*&      Form  generar_cf
*&---------------------------------------------------------------------*
*       Cargo la tabla interna con el código fuente del reporte.
*----------------------------------------------------------------------*
FORM generar_cf .

  DATA: first     TYPE i,
        numrows   TYPE i,
        rownum    TYPE i,
        mystring  TYPE string,
        crows(8)  TYPE c.

  DATA:
    l_index LIKE sy-tabix.

* Nota: Se toma como linea para el código fuente, 72 caracteres.

  MOVE 'ZSUBPOOL' TO prog.

  APPEND 'REPORT ZSUBPOOL.' TO code.
  APPEND '' TO code.

  IF gen IS NOT INITIAL.
    PERFORM generar_sel.
    PERFORM generar_start.
  ENDIF.

  APPEND 'Form DoSQL USING rows TYPE i.' TO code.
  APPEND '' TO code.

* Creo la tabla interna que contiene los datos.
  APPEND 'data : begin of TI_TAB occurs 0,' TO code.

  DESCRIBE TABLE tblcol_tab LINES numrows .

  CLEAR rownum.

  LOOP AT tblcol_tab.
    ADD 1 TO rownum.
    WRITE rownum TO crows.
    CONCATENATE 'WFLD' crows INTO mystring.
    CONDENSE mystring NO-GAPS.

    CONCATENATE mystring 'Like' tblcol_tab-tbl  INTO mystring
      SEPARATED BY space.

    IF rownum = numrows.
      CONCATENATE mystring '-' tblcol_tab-col '.' INTO mystring.
    ELSE.
      CONCATENATE mystring '-' tblcol_tab-col ',' INTO mystring.
    ENDIF.
    APPEND mystring TO code.
  ENDLOOP.
  APPEND 'data : end of TI_TAB.' TO code.
  APPEND '' TO code.

  APPEND 'Type-Pools : Slis.' TO code.
  APPEND '' TO code.
  APPEND 'DATA : l_repid LIKE sy-repid.'         TO code.
  APPEND 'DATA : fcat TYPE SLIS_T_FIELDCAT_ALV.' TO code.
  APPEND 'DATA : wcat LIKE LINE OF FCAT.'        TO code.
  APPEND 'DATA : MyString type STRING.'          TO code.
  APPEND 'DATA : MyTitle  type LVC_TITLE.'       TO code.
  APPEND '' TO code.

  APPEND 'MOVE sy-repid TO l_repid.'             TO code.

  APPEND '' TO code.

  IF vis IS NOT INITIAL.
    IF ti_param[] IS NOT INITIAL.

      LOOP AT ti_param WHERE tipo EQ 'P'.
* Defino las variables.

        CONCATENATE ti_param-tabname '-' ti_param-fieldname
          INTO mystring.
        CONCATENATE 'DATA : ' ti_param-param ' TYPE ' mystring '.'
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.
      ENDLOOP.
      IF sy-subrc IS INITIAL.
* Se encontraron parametros.
        APPEND '' TO code.

        APPEND 'DATA:'                                                TO code.
        APPEND 'l_ret     TYPE c,'                                    TO code.
        APPEND 'lt_fields TYPE sval OCCURS 0 WITH HEADER LINE.'       TO code.
        APPEND '' TO code.

        LOOP AT ti_param WHERE tipo EQ 'P'.
          APPEND 'MOVE:'                                                  TO code.
          CONCATENATE '''' ti_param-tabname ''''   ' TO lt_fields-tabname,' INTO mystring.
          APPEND mystring TO code.

          CONCATENATE '''' ti_param-fieldname ''''  ' TO lt_fields-fieldname,' INTO mystring.
          APPEND mystring TO code.
          CONCATENATE '''' ti_param-fieldname '''' ' TO lt_fields-fieldtext.' INTO mystring.
          APPEND mystring TO code.
          APPEND 'APPEND lt_fields.'                                    TO code.
        ENDLOOP.

        APPEND '' TO code.

        APPEND 'CALL FUNCTION ''POPUP_GET_VALUES'''         TO code.
        APPEND '    EXPORTING '                             TO code.
        APPEND '      popup_title     = ''Ingrese datos:''' TO code.
        APPEND '    IMPORTING '                             TO code.
        APPEND '      returncode      = l_ret '             TO code.
        APPEND '    TABLES '                                TO code.
        APPEND '      fields          = lt_fields '         TO code.
        APPEND '    EXCEPTIONS '                            TO code.
        APPEND '      error_in_fields = 1 '                 TO code.
        APPEND '      OTHERS          = 2. '                TO code.

        APPEND '' TO code.

        CLEAR l_index.

        LOOP AT ti_param WHERE tipo EQ 'P'.
* Asigno el valor del parámetro a la variable.
          APPEND '' TO code.
          ADD 1 TO l_index.
          CLEAR mystring.
          MOVE l_index TO mystring.
          CONCATENATE 'read table lt_fields index ' mystring '.'
            INTO mystring SEPARATED BY space.
          APPEND mystring TO code.

          CONCATENATE 'move lt_fields-value to ' ti_param-param '.'
            INTO mystring SEPARATED BY space.
          APPEND mystring TO code.
        ENDLOOP.

      ENDIF.

      LOOP AT ti_param WHERE tipo EQ 'S'.
* Hago la definición de los RANGES.
        APPEND '' TO code.
        CONCATENATE 'DATA: BEGIN OF' ti_param-param 'OCCURS 0,'
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.
        APPEND '     SIGN   TYPE RSDSSELOPT-SIGN, '                TO code.
        APPEND '     OPTION TYPE RSDSSELOPT-OPTION, '              TO code.
* LOW
        CONCATENATE ti_param-tabname '-' ti_param-fieldname
          INTO mystring.
        CONCATENATE '     LOW TYPE' mystring ','
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.
* HIGH
        CONCATENATE ti_param-tabname '-' ti_param-fieldname
          INTO mystring.
        CONCATENATE '     HIGH TYPE' mystring ','
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.

        CONCATENATE 'END OF' ti_param-param '.'
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.

      ENDLOOP.
      IF sy-subrc IS INITIAL.
* Se encontraron select-option.
        APPEND '' TO code.
        APPEND '    TYPES: BEGIN OF y_s_selopt45, '              TO code.
        APPEND '             sign(1),             '              TO code.
        APPEND '             option(2),           '              TO code.
        APPEND '             low  TYPE rsdslow,   '              TO code.
        APPEND '             high TYPE rsdslow,   '              TO code.
        APPEND '           END OF y_s_selopt45.   '              TO code.
        APPEND '' TO code.
        APPEND '    DATA:'              TO code.
        APPEND '      l_selopt TYPE y_s_selopt45,'                TO code.
        APPEND '      l_t_selopt TYPE TABLE OF y_s_selopt45,'     TO code.
        APPEND '      l_rstabfield TYPE rstabfield.'              TO code.

        LOOP AT ti_param WHERE tipo EQ 'S'.

          APPEND '' TO code.

          APPEND 'MOVE:'                                          TO code.
          CONCATENATE '''' ti_param-tabname ''''   ' TO l_rstabfield-tablename,'
            INTO mystring.
          APPEND mystring TO code.

          CONCATENATE '''' ti_param-fieldname ''''  ' TO l_rstabfield-fieldname.'
            INTO mystring.
          APPEND mystring TO code.

          APPEND '' TO code.

          APPEND 'CALL FUNCTION ''COMPLEX_SELECTIONS_DIALOG'''  TO code.
          APPEND '    EXPORTING '                               TO code.
          CONCATENATE '      title             = '  '''' ti_param-param ''''
            INTO mystring SEPARATED BY space.
          APPEND mystring TO code.
          APPEND '      tab_and_field     = l_rstabfield'       TO code.
          APPEND '    TABLES'                                   TO code.
          APPEND '      range             = l_t_selopt'         TO code.
          APPEND '    EXCEPTIONS'                               TO code.
          APPEND '    no_range_tab      = 1'                    TO code.
          APPEND '    cancelled         = 2'                    TO code.
          APPEND '    internal_error    = 3'                    TO code.
          APPEND '    invalid_fieldname = 4'                    TO code.
          APPEND '    OTHERS            = 5.'                   TO code.

          APPEND '' TO code.

* Paso la selección del usuario al RANGES.
          APPEND 'LOOP AT l_t_selopt INTO l_selopt.' TO code.

          CONCATENATE 'MOVE-CORRESPONDING l_selopt TO ' ti_param-param '.'
            INTO mystring SEPARATED BY space.
          APPEND mystring TO code.

          CONCATENATE 'APPEND ' ti_param-param '.'
            INTO mystring SEPARATED BY space.
          APPEND mystring TO code.

          APPEND 'ENDLOOP.' TO code.
          APPEND '' TO code.

        ENDLOOP.
      ENDIF.

    ENDIF.
  ENDIF.

  APPEND '' TO code.

  APPEND '* Create Field Catalogue' TO code.

  CLEAR rownum.

  LOOP AT tblcol_tab.
    ADD 1 TO rownum.
    WRITE rownum TO crows.
    CONCATENATE 'WFLD' crows INTO mystring.
    CONDENSE mystring NO-GAPS.

    CONCATENATE '    wcat-fieldname = ' '''' mystring '''' '.' INTO
mystring.
    APPEND mystring TO code.
    CONCATENATE '    wcat-tabname   = ' '''TI_TAB''' '.'
INTO mystring.
    APPEND mystring TO code.
    CONCATENATE '    wcat-ref_fieldname = ' '''' tblcol_tab-col
    '''.' INTO mystring.
    APPEND mystring TO code.
    CONCATENATE '    wcat-ref_tabname   = ' '''' tblcol_tab-tbl
    '''.' INTO mystring.
    APPEND mystring TO code.

    IF conv IS NOT INITIAL.
* Se setea el catálogo para que no realice la conversión definida
* en el dominio del campo.
      CONCATENATE '    wcat-no_convext = ' '''X''' '.'
        INTO mystring.
    ELSE.
      CONCATENATE '    wcat-no_convext = ' ''' ''' '.'
        INTO mystring.
    ENDIF.

    APPEND mystring TO code.

    IF tec IS NOT INITIAL.
* Se visualiza el nombre técnico de la columna.

      CONCATENATE '    wcat-seltext_l = ' 'wcat-seltext_m ='
        INTO mystring SEPARATED BY space.
      APPEND mystring TO code.

      CONCATENATE '    wcat-seltext_s = ' 'wcat-reptext_ddic = '
        INTO mystring SEPARATED BY space.
      APPEND mystring TO code.

      CONCATENATE '    ''' tblcol_tab-col '''.'
        INTO mystring.
      APPEND mystring TO code.

    ENDIF.

    APPEND '    Append Wcat to Fcat.' TO code.
    APPEND '' TO code.
  ENDLOOP.

  APPEND '' TO code.
  APPEND 'Try.' TO code.
  APPEND '' TO code.

  MOVE 0 TO first.

  LOOP AT ti_select INTO myline.
    APPEND myline TO code.
  ENDLOOP.

  IF l_single IS INITIAL.
    APPEND 'Into Table TI_TAB' TO code.
  ELSE.
    APPEND 'Into TI_TAB' TO code.
  ENDIF.

  IF NOT rows IS INITIAL AND l_single IS INITIAL.
    APPEND 'Up To ROWS rows' TO code.
  ENDIF.

  LOOP AT ti_from INTO myline.
    APPEND myline TO code.
  ENDLOOP.

  LOOP AT ti_where INTO myline.
    APPEND myline TO code.
  ENDLOOP.
  APPEND '.' TO code.
  APPEND '' TO code.

  IF NOT l_single IS INITIAL.
    APPEND 'APPEND TI_TAB.' TO code.
  ENDIF.

  APPEND '' TO code.
  APPEND 'CALL FUNCTION ''REUSE_ALV_LIST_DISPLAY'''           TO code.
  APPEND '       EXPORTING'                                   TO code.
  APPEND '            IT_FIELDCAT             = FCAT'         TO code.
  APPEND '       TABLES'                                      TO code.
  APPEND '            T_OUTTAB                = TI_TAB.'      TO code.
  APPEND '          .'                                        TO code.
  APPEND '' TO code.
  APPEND 'Catch CX_ROOT.' TO code.

  APPEND 'CALL FUNCTION ''POPUP_TO_INFORM'''                   TO code.
  APPEND '   EXPORTING'                                        TO code.
  APPEND '        titel = ''jncDynamicSub'''                   TO code.
  APPEND '        txt2  = ''Generate SUBROUTINE POOL Succeeded BUT SQL failed'''
                                                               TO code.
  APPEND '        txt1  = ''Possible Wrong SQL - see c:\jnc.ab4''.' TO
code.
  APPEND 'EndTry.'             TO code.
  APPEND 'EndForm.  "DoSQL.'   TO code.


ENDFORM.                    " generar_cf

*&---------------------------------------------------------------------*
*&      Form  set_screen
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM set_screen .

  LOOP AT SCREEN.
    IF screen-name = 'REPID'.
      IF vis IS NOT INITIAL.
        screen-active = '0'.    " Input/Output/Invisible
        CLEAR text7.
      ELSE.
        screen-active = '1'.
        text7 = 'Reporte'.
      ENDIF.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.

ENDFORM.                    " set_screen

*&---------------------------------------------------------------------*
*&      Form  generar_sel
*&---------------------------------------------------------------------*
*       Creo la pantalla del reporte.
*----------------------------------------------------------------------*
FORM generar_sel .

  DATA:
    l_field   TYPE string,
    mystring  TYPE string.

  APPEND '' TO code.

  LOOP AT ti_param WHERE tipo EQ 'S'.
* Defino las tablas.
    CONCATENATE 'TABLES' ti_param-tabname '.'
      INTO mystring SEPARATED BY space.
    APPEND mystring TO code.
  ENDLOOP.

  APPEND '' TO code.

  LOOP AT ti_param.
* Defino la pantalla.
    CONCATENATE ti_param-tabname '-' ti_param-fieldname
      INTO l_field.

    CASE  ti_param-tipo.
      WHEN 'S'.
* SELECT-OPTIONS
        CONCATENATE 'SELECT-OPTIONS' ti_param-param 'FOR' l_field '.'
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.

      WHEN 'P'.
* PARAMETERS
        CONCATENATE 'PARAMETERS' ti_param-param 'LIKE' l_field '.'
          INTO mystring SEPARATED BY space.
        APPEND mystring TO code.

      WHEN OTHERS.
* DO NOTHING !!
    ENDCASE.

  ENDLOOP.

  IF rows IS INITIAL.
    APPEND 'PARAMETER: rows   TYPE i NO-DISPLAY . ' TO code.
  ELSE.
    APPEND 'PARAMETER: rows   TYPE i.   "Up To N rows' TO code.
  ENDIF.

ENDFORM.                    " generar_sel

*&---------------------------------------------------------------------*
*&      Form  generar_start
*&---------------------------------------------------------------------*
*       Genero el START-OF-SELECTION y la llamada al form.
*----------------------------------------------------------------------*
FORM generar_start .

  APPEND '' TO code.
  APPEND 'START-OF-SELECTION.' TO code.
  APPEND '' TO code.
  APPEND '  PERFORM dosql USING rows.' TO code.
  APPEND '' TO code.

ENDFORM.                    " generar_start

*&---------------------------------------------------------------------*
*&      Form  generar_subrutina
*&---------------------------------------------------------------------*
*       Genero la subrutina.
*----------------------------------------------------------------------*
FORM generar_subrutina .

  GENERATE SUBROUTINE POOL code   NAME    prog
                                  MESSAGE msg
                                  LINE    lin
                                  WORD    wrd
                                  OFFSET  off.

  IF sy-subrc <> 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = g_repid
        txt2  = 'Generate SUBROUTINE POOL Failed'
        txt1  = msg.
  ELSE.

    PERFORM dosql IN PROGRAM (prog)
                  USING rows IF FOUND.

    IF sy-subrc <> 0.
      CALL FUNCTION 'POPUP_TO_INFORM'
        EXPORTING
          titel = g_repid
          txt2  = 'Generate SUBROUTINE POOL Succeeded BUT Call failed'
          txt1  = 'Possible Wrong SQL - see c:\jnc.ab4'.
    ENDIF.
  ENDIF.

ENDFORM.                    " generar_subrutina

*&---------------------------------------------------------------------*
*&      Form  generar_reporte
*&---------------------------------------------------------------------*
*       Genero el reporte.
*----------------------------------------------------------------------*
FORM generar_reporte .

  CONSTANTS:
    rs_c_true TYPE rs_bool VALUE 'X'.

  DATA:
    l_msg   TYPE string.

  CONCATENATE 'Reporte creado: ' repid INTO l_msg SEPARATED BY space.

  MESSAGE l_msg TYPE 'I'.

*  CHECK sy-uname EQ 'ABAPD4JP' OR sy-uname EQ 'D4JP'.

  INSERT REPORT repid FROM code
                      PROGRAM TYPE '1'.
*                      UNICODE ENABLING rs_c_true.

  CHECK sy-subrc IS INITIAL.

  COMMIT WORK.

  SUBMIT (repid) VIA SELECTION-SCREEN AND RETURN.

ENDFORM.                    " generar_reporte

*&---------------------------------------------------------------------*
*&      Form  validar_repid
*&---------------------------------------------------------------------*
*       Valido que no exista el reporte que se quiere crear.
*----------------------------------------------------------------------*
FORM validar_repid .

  DATA:
    l_r3state  TYPE reposrc-r3state,
    l_obj_name TYPE tadir-obj_name.

  CHECK gen IS NOT INITIAL.

  IF repid IS INITIAL.
    MESSAGE 'Ingrese nombre del reporte.' TYPE 'I'.
    STOP.
  ENDIF.

* Verifico que genere un reporte dentro de la nomenclatura del cliente.
  IF repid(1) NE 'Z' AND repid(1) NE 'Y'.
    MESSAGE 'Nombre del reporte erroneo.' TYPE 'I'.
    STOP.
  ENDIF.

* Verifico si el nombre del reporte ya existe, para evitar que pise el
* código fuente generado con anterioridad.
*  SELECT SINGLE obj_name
*    INTO l_obj_name
*    FROM tadir
*    WHERE pgmid    EQ 'R3TR'
*      AND object   EQ 'PROG'
*      AND obj_name EQ repid.

  SELECT SINGLE r3state
    INTO l_r3state
    FROM reposrc
    WHERE progname EQ repid.

  CHECK sy-subrc IS INITIAL.

  MESSAGE 'Nombre del reporte existente.' TYPE 'I'.
  STOP.

ENDFORM.                    " validar_repid

*&---------------------------------------------------------------------*
*&      Form  generar_repid
*&---------------------------------------------------------------------*
*       Creo el nombre del reporte.
*----------------------------------------------------------------------*
FORM generar_repid .

  CHECK repid IS INITIAL.

  CONCATENATE 'ZZ' sy-uname '_' sy-datum '_' sy-timlo INTO repid.

ENDFORM.                    " generar_repid


Last edited by admin on Tue Nov 18, 2008 1:31 am; edited 1 time in total
Back to top
View user's profile Send private message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Tue Nov 18, 2008 1:28 am    Post subject: Reply with quote

Code:
*&---------------------------------------------------------------------*
*& Report  ZZSQL_QUERY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zzsql_query.

TYPE-POOLS:
  icon,
  rsfs,
  rsds,
  slis.

TABLES:
  sscrfields.       "Campos en las imágenes de selección

DATA:
  g_repid   LIKE sy-repid.

DATA:
      delim     TYPE c.

DATA: code      TYPE TABLE OF rssource-line,
      prog(8)   TYPE c,
      msg(120)  TYPE c,
      lin(3)    TYPE c,
      wrd(10)   TYPE c,
      off(3)    TYPE c.

DATA: onelinecode LIKE LINE OF code.

DATA : fcat TYPE slis_t_fieldcat_alv.
DATA : wcat LIKE LINE OF fcat.

CONSTANTS: c_line_length TYPE i VALUE 80.

* define table type for data exchange
TYPES: BEGIN OF mytable_line,
         line(c_line_length) TYPE c,
       END OF mytable_line.

TYPES: ty_table TYPE TABLE OF mytable_line.

DATA: BEGIN OF ti_dd03l OCCURS 0,
         tabname   LIKE dd03l-tabname,
         fieldname LIKE dd03l-fieldname,
         position  LIKE dd03l-position,
      END OF ti_dd03l.

* Tablas con los datos de la consulta.
DATA:
  ti_select TYPE ty_table,
  ti_from   TYPE ty_table,
  ti_where  TYPE ty_table,
  ti_tmp    TYPE ty_table.

DATA:
  myline LIKE LINE OF ti_select.

DATA:
  functxt  TYPE smp_dyntxt.

*----------------------------------------------------------------------*
* PARAMETROS
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK bl01 WITH FRAME TITLE tit .

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON 10(45) sel USER-COMMAND sel
                                        VISIBLE LENGTH 12.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON 10(45) frm USER-COMMAND frm
                                        VISIBLE LENGTH 12.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON 10(45) whr USER-COMMAND whr
                                        VISIBLE LENGTH 12.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN END   OF BLOCK bl01.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(15) text1,
                  POSITION 20.
PARAMETER: rows   TYPE i.        "Up To N rows
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(15) text2,
                  POSITION 20.
PARAMETER: down   AS CHECKBOX.   "Download
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(16) text3,
                  POSITION 20.
PARAMETER: conv   AS CHECKBOX.   "Conversión
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(16) text4,
                  POSITION 20.
PARAMETER: tec   AS CHECKBOX.   "Nombre técnico
SELECTION-SCREEN: END OF LINE.

* Botón de Borrar todo en el STATUS GUI.
SELECTION-SCREEN: FUNCTION KEY 1.

INITIALIZATION.

  tit   = 'Parámetros:'.
  text1 = 'Up To N rows'.
  text2 = 'Download code'.
  text3 = 'Sin conv. x dom.'.
  text4 = 'Nombre técnico'.

* Agrego el boton de borrar todo en STATUS GUI.
  functxt-icon_id   = icon_delete.
  functxt-quickinfo = 'Borrar todo'.
  sscrfields-functxt_01 = functxt.

* Creo la visualización del botón de SELECT en la DYNPRO.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_select_detail
      text   = 'SELECT'
      info   = 'Visualizar SELECT'
    IMPORTING
      RESULT = sel
    EXCEPTIONS
      OTHERS = 0.

* Creo la visualización del botón de FROM en la DYNPRO.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_table_settings
      text   = 'FROM'
      info   = 'Visualizar FROM'
    IMPORTING
      RESULT = frm
    EXCEPTIONS
      OTHERS = 0.

* creo la visualización del botón de WHERE en la dynpro.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_filter
      text   = 'WHERE'
      info   = 'Visualizar WHERE'
    IMPORTING
      RESULT = whr
    EXCEPTIONS
      OTHERS = 0.

  IMPORT ti_select FROM MEMORY ID 'ZSELECT'.
  IMPORT ti_from   FROM MEMORY ID 'ZFROM'.
  IMPORT ti_where  FROM MEMORY ID 'ZWHERE'.

*-----------------------------------------------------------------------
* AT SELECTION-SCREEN
*-----------------------------------------------------------------------

AT SELECTION-SCREEN.

  CASE sy-ucomm.
    WHEN 'FC01'.
      REFRESH: ti_select, ti_where, ti_from.
      FREE MEMORY ID: 'ZSELECT', 'ZFROM', 'ZWHERE'.
    WHEN 'SEL'.
      PERFORM editor_table  USING ' '
                                  'SELECT'
                         CHANGING ti_select[].
    WHEN 'FRM'.
      PERFORM editor_table  USING ' '
                                  'FROM'
                         CHANGING ti_from[].
    WHEN 'WHR'.
      PERFORM editor_table  USING ' '
                                  'WHERE'
                         CHANGING ti_where[].
    WHEN OTHERS.
* Do Nothing !!.
  ENDCASE.

START-OF-SELECTION.

  PERFORM f_runsql.


************************************************************************
*  F O R M S
************************************************************************

*&---------------------------------------------------------------------*
*&      Form  F_RUNSQL
*&---------------------------------------------------------------------*
FORM f_runsql.

  DATA: first     TYPE i,
        numcols   TYPE i,
        aggfun    TYPE i,
        pos       TYPE i,
        off       TYPE i,
        len       TYPE i,
        numrows   TYPE i,
        rownum    TYPE i,
        mystring  TYPE string,
        mystring2 TYPE string,
        crows(8)  TYPE c.

  DATA:
    l_tfill TYPE sy-tfill,
    l_single TYPE c,
    l_todos TYPE c,
    l_tabix LIKE sy-tabix,
    l_carac TYPE c,
    l_table LIKE rsrd1-tbma_val,
    l_join  TYPE c,
    l_cant  TYPE i.

  DATA : BEGIN OF ti_val OCCURS 0,
             val(80),
         END OF ti_val.


  DATA : BEGIN OF tblcol_tab OCCURS 0,
             tbl TYPE string,
             col TYPE string.
  DATA : END OF tblcol_tab.

  FIELD-SYMBOLS: <fs> TYPE mytable_line.

  CHECK ti_select[] IS NOT INITIAL AND ti_from[] IS NOT INITIAL.

  EXPORT ti_select TO MEMORY ID 'ZSELECT'.
  EXPORT ti_from   TO MEMORY ID 'ZFROM'.
  EXPORT ti_where  TO MEMORY ID 'ZWHERE'.

  CLEAR:  code, l_join, l_table, l_todos.
  REFRESH code.

  MOVE 0 TO : first, numcols, aggfun.

* #####################
* ####### FROM ########
* #####################

* Verifico si se realiza un JOIN.
  SEARCH ti_from FOR 'JOIN'.

  IF sy-subrc EQ 0.
    l_join = 'X'.
    CLEAR l_table.
  ENDIF.

  IF l_join IS INITIAL.
* Busco el nombre de la tabla.
    SEARCH ti_from FOR 'FROM'.

    IF sy-subrc EQ 0.

      l_tabix = sy-tabix.

      DO.
        READ TABLE ti_from INTO myline INDEX l_tabix.
        IF sy-subrc NE 0.
          EXIT.
        ENDIF.
        TRANSLATE myline TO UPPER CASE.
        CONDENSE myline.
        REFRESH ti_val.
        SPLIT myline AT space INTO TABLE ti_val.

        LOOP AT ti_val WHERE val NE 'FROM'.
          l_table = ti_val-val.
          EXIT.
        ENDLOOP.

        IF sy-subrc EQ 0.
          EXIT.
        ELSE.
          ADD 1 TO l_tabix.
        ENDIF.
      ENDDO.
    ELSE.
      CALL FUNCTION 'POPUP_TO_INFORM'
        EXPORTING
          titel = g_repid
          txt2  = 'FROM DMLs Only Please!'
          txt1  = 'Correct & Retry'.

      STOP.
    ENDIF.
  ENDIF.

* #####################
* ###### SELECT #######
* #####################
  ti_tmp[] = ti_select[].

* Busco el select.
  SEARCH ti_tmp FOR 'SELECT'.

  IF sy-subrc NE 0.
* Falta el SELECT.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = g_repid
        txt2  = 'SELECT DMLs Only Please!'
        txt1  = 'Correct & Retry'.

    STOP.
  ELSE.
    READ TABLE ti_tmp INTO myline INDEX sy-tabix.
    TRANSLATE myline TO UPPER CASE.
    REPLACE 'SELECT' IN myline WITH space.
    CONDENSE myline.
    MODIFY ti_tmp FROM myline INDEX sy-tabix.
  ENDIF.

  SEARCH ti_tmp FOR 'SINGLE'.

  IF sy-subrc EQ 0.
* Se trata de un SELECT SINGLE.
    l_single = 'X'.
    READ TABLE ti_tmp INTO myline INDEX sy-tabix.
    TRANSLATE myline TO UPPER CASE.
    REPLACE 'SINGLE' IN myline WITH space.
    CONDENSE myline.
    MODIFY ti_tmp FROM myline INDEX sy-tabix.
  ELSE.
    CLEAR l_single.
  ENDIF.

  IF l_join IS INITIAL.
    LOOP AT ti_tmp INTO myline.
      CONDENSE myline.
      off = STRLEN( myline ).
      IF off = 0.
        CONTINUE.
      ELSE.
* Verifico si se requieren todos los campos de la tabla.
        DO.
          l_carac = myline+pos(1).
          IF l_carac = '*'.
* Tengo que buscar todos los campos de la tabla para poder
* visualizarlos.
            PERFORM buscar_campos TABLES ti_dd03l
                                  USING  l_table.
            l_todos = 'X'.
            EXIT.
          ELSE.
            IF pos < off.
              ADD 1 TO pos.
            ELSE.
              EXIT.
            ENDIF.
          ENDIF.
        ENDDO.
      ENDIF.
    ENDLOOP.
  ENDIF.

  IF l_todos IS INITIAL.
    LOOP AT ti_tmp INTO myline.
      IF l_join EQ 'X'.
*      '~'
        TRANSLATE myline TO UPPER CASE.
        CONDENSE myline.
        REFRESH ti_val.
        SPLIT myline AT space INTO TABLE ti_val.
        LOOP AT ti_val WHERE val NE space.
          SPLIT ti_val-val AT '~' INTO tblcol_tab-tbl tblcol_tab-col.
          APPEND tblcol_tab.
        ENDLOOP.
      ELSE.
* Se seleccionaron varios campos.
        TRANSLATE myline TO UPPER CASE.
        CONDENSE myline.
        REFRESH ti_val.
        SPLIT myline AT space INTO TABLE ti_val.

        LOOP AT ti_val WHERE val NE space.
          tblcol_tab-tbl = l_table.
          tblcol_tab-col = ti_val-val.
          APPEND tblcol_tab.
        ENDLOOP.
      ENDIF.
    ENDLOOP.
  ELSE.
* Cargo los campos del '*' (Asterisco).

    DESCRIBE TABLE ti_dd03l LINES l_tfill.

*    IF l_tfill = 99.
    LOOP AT ti_select ASSIGNING <fs>.
* Elimino el '*' (Asterisco), ya que solo se pueden visualizar
* 99 campos en un ALV.
      TRANSLATE <fs>-line USING '* '.
    ENDLOOP.
*    ENDIF.

    REFRESH tblcol_tab.
    LOOP AT ti_dd03l.
      tblcol_tab-tbl = ti_dd03l-tabname.
      tblcol_tab-col = ti_dd03l-fieldname.
      APPEND tblcol_tab.

*      CHECK l_tfill = 99.
* Cargo los campos, ya que saque el '*' (Asterisco).
      APPEND ti_dd03l-fieldname TO ti_select.
    ENDLOOP.
  ENDIF.

  IF tblcol_tab[] IS INITIAL.
* No hay ningún campo seleccionado.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = g_repid
        txt2  = 'Table~Column Open SQL is MUST'
        txt1  = 'No Fields'.
    STOP.
  ENDIF.

* ###################################
* ###### Genero del REPORT ##########
* ###################################

* Nota: Se toma como linea para el código fuente, 72 caracteres.

  MOVE 'ZSUBPOOL' TO prog.

  APPEND 'REPORT ZSUBPOOL.' TO code.
  APPEND '' TO code.
  APPEND 'Form DoSQL USING rows TYPE i.' TO code.
  APPEND '' TO code.

* Creo la tabla interna que contiene los datos.
  APPEND 'data : begin of TI_TAB occurs 0,' TO code.

  DESCRIBE TABLE tblcol_tab LINES numrows .

  CLEAR rownum.

  LOOP AT tblcol_tab.
    ADD 1 TO rownum.
    WRITE rownum TO crows.
    CONCATENATE 'WFLD' crows INTO mystring.
    CONDENSE mystring NO-GAPS.

    CONCATENATE mystring 'Like' tblcol_tab-tbl  INTO mystring
      SEPARATED BY space.

    IF rownum = numrows.
      CONCATENATE mystring '-' tblcol_tab-col '.' INTO mystring.
    ELSE.
      CONCATENATE mystring '-' tblcol_tab-col ',' INTO mystring.
    ENDIF.
    APPEND mystring TO code.
  ENDLOOP.
  APPEND 'data : end of TI_TAB.' TO code.
  APPEND '' TO code.

  APPEND 'Type-Pools : Slis.' TO code.
  APPEND '' TO code.
  APPEND 'DATA : l_repid LIKE sy-repid.'         TO code.
  APPEND 'DATA : fcat TYPE SLIS_T_FIELDCAT_ALV.' TO code.
  APPEND 'DATA : wcat LIKE LINE OF FCAT.'        TO code.
  APPEND 'DATA : MyString type STRING.'          TO code.
  APPEND 'DATA : MyTitle  type LVC_TITLE.'       TO code.
  APPEND '' TO code.

  APPEND 'MOVE sy-repid TO l_repid.'             TO code.

  APPEND '' TO code.

  APPEND '* Create Field Catalogue' TO code.

  CLEAR rownum.

  LOOP AT tblcol_tab.
    ADD 1 TO rownum.
    WRITE rownum TO crows.
    CONCATENATE 'WFLD' crows INTO mystring.
    CONDENSE mystring NO-GAPS.

    CONCATENATE '    wcat-fieldname = ' '''' mystring '''' '.' INTO
mystring.
    APPEND mystring TO code.
    CONCATENATE '    wcat-tabname   = ' '''TI_TAB''' '.'
INTO mystring.
    APPEND mystring TO code.
    CONCATENATE '    wcat-ref_fieldname = ' '''' tblcol_tab-col
    '''.' INTO mystring.
    APPEND mystring TO code.
    CONCATENATE '    wcat-ref_tabname   = ' '''' tblcol_tab-tbl
    '''.' INTO mystring.
    APPEND mystring TO code.

    IF conv IS NOT INITIAL.
* Se setea el catálogo para que no realice la conversión definida
* en el dominio del campo.
      CONCATENATE '    wcat-no_convext = ' '''X''' '.'
        INTO mystring.
    ELSE.
      CONCATENATE '    wcat-no_convext = ' ''' ''' '.'
        INTO mystring.
    ENDIF.

    APPEND mystring TO code.

    IF tec IS NOT INITIAL.
* Se visualiza el nombre técnico de la columna.

      CONCATENATE '    wcat-seltext_l = ' 'wcat-seltext_m ='
        INTO mystring SEPARATED BY space.
      APPEND mystring TO code.

      CONCATENATE '    wcat-seltext_s = ' 'wcat-reptext_ddic = '
        INTO mystring SEPARATED BY space.
      APPEND mystring TO code.

      CONCATENATE '    ''' tblcol_tab-col '''.'
        INTO mystring.
      APPEND mystring TO code.

    ENDIF.

    APPEND '    Append Wcat to Fcat.' TO code.

  ENDLOOP.

  APPEND '' TO code.
  APPEND 'Try.' TO code.
  APPEND '' TO code.

  MOVE 0 TO first.

  LOOP AT ti_select INTO myline.
    APPEND myline TO code.
  ENDLOOP.

  IF l_single IS INITIAL.
    APPEND 'Into Table TI_TAB' TO code.
  ELSE.
    APPEND 'Into TI_TAB' TO code.
  ENDIF.

  IF NOT rows IS INITIAL AND l_single IS INITIAL.
    APPEND 'Up To ROWS rows' TO code.
  ENDIF.

  LOOP AT ti_from INTO myline.
    APPEND myline TO code.
  ENDLOOP.

  LOOP AT ti_where INTO myline.
    APPEND myline TO code.
  ENDLOOP.
  APPEND '.' TO code.
  APPEND '' TO code.

  IF NOT l_single IS INITIAL.
    APPEND 'APPEND TI_TAB.' TO code.
  ENDIF.

  APPEND '' TO code.
  APPEND 'CALL FUNCTION ''REUSE_ALV_LIST_DISPLAY'''           TO code.
  APPEND '       EXPORTING'                                   TO code.
  APPEND '            IT_FIELDCAT             = FCAT'         TO code.
  APPEND '       TABLES'                                      TO code.
  APPEND '            T_OUTTAB                = TI_TAB.'      TO code.
  APPEND '          .'                                        TO code.
  APPEND '' TO code.
  APPEND 'Catch CX_ROOT.' TO code.

  APPEND 'CALL FUNCTION ''POPUP_TO_INFORM'''                   TO code.
  APPEND '   EXPORTING'                                        TO code.
  APPEND '        titel = ''jncDynamicSub'''                   TO code.
  APPEND '        txt2  = ''Generate SUBROUTINE POOL Succeeded BUT SQL failed'''
                                                               TO code.
  APPEND '        txt1  = ''Possible Wrong SQL - see c:\jnc.ab4''.' TO
code.
  APPEND 'EndTry.'             TO code.
  APPEND 'EndForm.  "DoSQL.'   TO code.

  IF down IS NOT INITIAL.
* Download del código fuente.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename = 'C:\SQL.txt'
      TABLES
        data_tab = code.
  ENDIF.

  GENERATE SUBROUTINE POOL code   NAME    prog
                                  MESSAGE msg
                                  LINE    lin
                                  WORD    wrd
                                  OFFSET  off.

  IF sy-subrc <> 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = g_repid
        txt2  = 'Generate SUBROUTINE POOL Failed'
        txt1  = msg.
  ELSE.
    PERFORM dosql IN PROGRAM (prog)
                  USING rows IF FOUND.
    IF sy-subrc <> 0.
      CALL FUNCTION 'POPUP_TO_INFORM'
        EXPORTING
          titel = g_repid
          txt2  = 'Generate SUBROUTINE POOL Succeeded BUT Call failed'
          txt1  = 'Possible Wrong SQL - see c:\jnc.ab4'.
    ENDIF.
  ENDIF.

ENDFORM.        "F_RUNSQL

*&---------------------------------------------------------------------*
*&      Form  buscar_campos
*&---------------------------------------------------------------------*
*       Busco todos los campos de la tabla seleccionada.
*----------------------------------------------------------------------*
*      -->PT_DD03L  Tabla con los campos.
*      -->P_TABLE   Tabla.
*----------------------------------------------------------------------*
FORM buscar_campos  TABLES   pt_dd03l STRUCTURE ti_dd03l
                    USING    p_table.

  SELECT tabname fieldname position
    INTO CORRESPONDING FIELDS OF TABLE pt_dd03l
    FROM dd03l
    UP TO 99 ROWS
    WHERE tabname       EQ p_table
      AND NOT fieldname LIKE '.INCLU%'
      AND datatype      NE 'CLNT'.

  SORT pt_dd03l BY position.

ENDFORM.                    " buscar_campos

*&---------------------------------------------------------------------*
*&      Form  editor_table
*&---------------------------------------------------------------------*
*       Editor de texto.
*----------------------------------------------------------------------*
*      -->PT_LINES  Líneas del texto.
*      -->P_DISPLAY Visualizar o editar.
*----------------------------------------------------------------------*
FORM editor_table  USING    p_display TYPE s38e-app_disp
                            p_name    TYPE trdir-name
*                   CHANGING pt_lines  TYPE mytable_line.
                    CHANGING pt_lines  TYPE ty_table.

  DATA:
    l_changed LIKE s38e-buf_varied,
    l_subrc   LIKE sy-subrc.

  CALL FUNCTION 'EDITOR_TABLE'
    EXPORTING
      display = p_display
      name    = p_name
    IMPORTING
      changed = l_changed
      subrc   = l_subrc
    TABLES
      content = pt_lines.

ENDFORM.                    " editor_table
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.