SELECT dinamica AYUDA¡¡¡

Colapsar
X
 
  • Tiempo
  • Mostrar
Limpiar Todo
nuevos mensajes
  • ibai23
    Junior Member
    • mar
    • 19

    #1

    SELECT dinamica AYUDA¡¡¡

    Hola ABAPeros,

    tengo la siguiente cuestión:

    En una tabla Z tengo un campo en el que está el nombre de otra tabla Z y tengo que ser capaz de hacer select a esas tablas Z pero dinamicamente, porque el nombre puede variar.

    ¿alguno ejemplillo rapido tienen alguien?

    Muchas gracias y un saludo
  • Jewel_1982
    Junior Member
    • nov
    • 20

    #2
    Vamos a ver, create un puntero y cuando vayas a hacer la select intenta leer en el where el contenido del puntero.

    Yo he hecho tablas dinamicas asi, pero nose intentalo a ver, voy a probarlo yo tb!

    Comentario

    • Garces
      Senior Member
      • ago
      • 321

      #3
      Otra que puedes hacer es usar la dunctión RFC_READ TABLE...
      Te adjunto un ejemplo...
      Aquí uso la función en una consulta remota, pero también funciona en consultas locales...

      Código:
      *&----------------------------------------------------------------------
      *&        FORM consulta_remota
      *&----------------------------------------------------------------------
      *&
      *&----------------------------------------------------------------------
      FORM consulta_remota.
      * Filtros a usar para la consulta remota.
      * Primer Filtro
        REFRESH t_option.
        LOOP AT r_ord_tr.
          t_option-sign   = r_ord_tr-sign.
          t_option-option = r_ord_tr-option.
          t_option-low    = r_ord_tr-low.
          t_option-high   = r_ord_tr-high.
          APPEND t_option.
        ENDLOOP.
        t_campo-fieldname = 'TRKORR'.    " Campo a filtrar
        t_campo-selopt_t  = t_option[].  " Filtro aplicado
        APPEND t_campo.
      * Segundo Filtro
        REFRESH t_option.
        LOOP AT r_fec_tr.
          t_option-sign   = r_fec_tr-sign.
          t_option-option = r_fec_tr-option.
          t_option-low    = r_fec_tr-low.
          t_option-high   = r_fec_tr-high.
          APPEND t_option.
        ENDLOOP.
        t_campo-fieldname = 'AS4DATE'.   " Campo a Filtrar
        t_campo-selopt_t  = t_option[].  " Filtro aplicado
        APPEND t_campo.
      * Tercer Filtro
        REFRESH t_option.
        t_option-sign   = 'I'.
        t_option-option = 'EQ'.
        t_option-low    = ''.
        t_option-high   = ''.
        APPEND t_option.
        t_campo-fieldname = 'STRKORR'.   " Campo a Filtrar
        t_campo-selopt_t  = t_option[].  " Filtro aplicado
        APPEND t_campo.
      * Tabla donde se buscará
        t_ranges-tablename = 'E070'.
        t_ranges-frange_t  = t_campo[].
        APPEND t_ranges.
        CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
          EXPORTING
            field_ranges  = t_ranges[]
          IMPORTING
            where_clauses = t_where[].
        READ TABLE t_where INDEX 1.
        IF sy-subrc = 0.
          t_select[] = t_where-where_tab[].
        ENDIF.
      * Campos a obtener remotamente.
        t_fields-fieldname = 'TRKORR'.
        APPEND t_fields.
        t_fields-fieldname = 'AS4DATE'.
        APPEND t_fields.
      * Llamo a la consulta dinámica
        CALL FUNCTION 'RFC_READ_TABLE'
          DESTINATION p_sis_de
          EXPORTING
            query_table          = 'E070'   " Tabla donde bas a buscar
            delimiter            = ' '
            no_data              = ' '
            rowskips             = 0
            rowcount             = 0
          TABLES
            OPTIONS              = t_select  " Filtros
            fields               = t_fields  " Campos a seleccionar
            data                 = t_data    " Tabla donde aparecerán los datos
          EXCEPTIONS
            table_not_available  = 1
            table_without_data   = 2
            option_not_valid     = 3
            field_not_valid      = 4
            not_authorized       = 5
            data_buffer_exceeded = 6
            OTHERS               = 7.
        IF sy-subrc <> 0.
          CASE sy-subrc.
            WHEN 1.
              sy-msgv1 = text-er1.
            WHEN 2.
              sy-msgv1 = text-er2.
            WHEN 3.
              sy-msgv1 = text-er3.
            WHEN 4.
              sy-msgv1 = text-er4.
            WHEN 5.
              sy-msgv1 = text-er5.
            WHEN 6.
              sy-msgv1 = text-er6.
            WHEN 7.
              sy-msgv1 = text-er7.
            WHEN OTHERS.
          ENDCASE.
          MESSAGE s000 WITH sy-msgv1.
        ELSE.
          t_result[] = t_data[].
          SORT t_result BY as4date ASCENDING
                           trkorr  ASCENDING.
          PERFORM llena_tabla.
        ENDIF.
      ENDFORM.                    "consulta_remota
      Miguel Ángel Garcés Ramírez

      Comentario

      Trabajando...