14/09/2010

OpenSQL WHERE Clauses - Interactive and Dynamic


REPORT ZRR_SAMPLE.

PARAMETERS dbtab TYPE tabname DEFAULT 'SPFLI'.

TYPE-POOLS rsds.

DATA : field_tab TYPE TABLE OF rsdsfields,
table_tab TYPE TABLE OF rsdstabs,
cond_tab TYPE rsds_twhere.

DATA : table LIKE LINE OF table_tab,
cond LIKE LINE OF cond_tab.

DATA : tadir_wa TYPE tadir,
selid TYPE rsdynsel-selid.

DATA : dref TYPE REF TO data,
alv TYPE REF TO cl_salv_table.

FIELD-SYMBOLS <table> TYPE STANDARD TABLE.

SELECT SINGLE *
FROM tadir
INTO tadir_wa
WHERE pgmid = 'R3TR' AND
object = 'TABL' AND
obj_name = dbtab.

IF sy-subrc <> 0.

MESSAGE 'Tabela Não Encontrada' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.

ENDIF.

table-prim_tab = dbtab.

APPEND table TO table_tab.

CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
kind = 'T'
IMPORTING
selection_id = selid
TABLES
tables_tab = table_tab
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.

MESSAGE 'Erro ao Executar a Inicialização' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.

ENDIF.

CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = selid
title = 'Seleção Dinamica'
as_window = ' '
IMPORTING
where_clauses = cond_tab
TABLES
fields_tab = field_tab
EXCEPTIONS
OTHERS = 4.

IF sy-subrc <> 0.

MESSAGE 'Erro ao Definir as Condições de Seleção' TYPE 'I'.
LEAVE PROGRAM.

ENDIF.

READ TABLE cond_tab WITH KEY tablename = dbtab INTO cond.

IF sy-subrc <> 0.

MESSAGE 'Todos os Registros serão' TYPE 'I'.

ENDIF.

CREATE DATA dref TYPE TABLE OF (dbtab).

ASSIGN dref->* TO <table>.

TRY.

SELECT *
FROM (dbtab)
INTO TABLE <table>
WHERE (cond-where_tab).

CATCH cx_sy_dynamic_osql_error.

MESSAGE 'Erro no Open SQL Dinamico' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.

ENDTRY.

TRY.

cl_salv_table=>factory( IMPORTING r_salv_table = alv
CHANGING t_table = <table> ).

alv->display( ).

CATCH cx_salv_msg.

MESSAGE 'Erro ao Criar o ALV' TYPE 'I' DISPLAY LIKE 'E'.

ENDTRY.

Nenhum comentário:

Postar um comentário