Advertisement
CDLG_TGR

Requête dynamique

Apr 19th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 3.19 KB | None | 0 0
  1.  
  2. FORM select_entries_to_duplicate
  3.     USING
  4.           it_tblmrg TYPE tt_tblmrgdup
  5.           iv_fldexcl TYPE wdy_boolean " si oui le critére de
  6.   " selection des champs rentre dans le SELECT
  7.     CHANGING
  8.           et_tbllin  TYPE tt_tbllindup
  9.           ct_protocole TYPE tt_protocole.
  10.  
  11.   DATA : ls_tblmrg TYPE ts_tblmrgdup.
  12.   DATA : ls_tbllin TYPE ts_tbllindup.
  13.  
  14.   DATA : lv_clause TYPE string,
  15.          lv_clkey  TYPE string,
  16.          lv_clfld  TYPE string.
  17.  
  18.   DATA : ls_key   TYPE ts_fldmrg,
  19.          ls_fld   TYPE ts_fldmrg,
  20.          ls_clval TYPE ts_clval.
  21.  
  22.   DATA : lv_index TYPE i.
  23.  
  24.   DATA: lv_value TYPE string.
  25.   DATA : lt_tblref TYPE REF TO data.
  26.   FIELD-SYMBOLS : <fs_tbl> TYPE table.
  27.  
  28.   LOOP AT it_tblmrg INTO ls_tblmrg.
  29.  
  30.     CLEAR lv_clkey.
  31.     CLEAR lv_clfld.
  32.     CLEAR lv_clause.
  33.     CLEAR ls_tbllin.
  34.  
  35.     " construire la clause where
  36.     LOOP AT ls_tblmrg-t_key INTO ls_key.
  37. *
  38. *      IF sy-tabix NE 1 AND lines( ls_key-t_clval ) NE 0.
  39. *        CONCATENATE lv_clause 'AND' INTO lv_clause SEPARATED BY space.
  40. *      ENDIF.
  41.       IF lines( ls_key-t_clval ) NE 0 AND sy-tabix NE 1.
  42.         lv_clkey = lv_clkey && ` AND `.
  43.       ENDIF.
  44.       IF lines( ls_key-t_clval ) NE 0.
  45.         lv_clkey = lv_clkey && ` ( `.
  46.       ENDIF.
  47.       LOOP AT ls_key-t_clval INTO ls_clval.
  48.         IF sy-tabix NE 1.
  49.           lv_clkey = lv_clkey && ` OR `.
  50.         ENDIF.
  51.         lv_clkey = lv_clkey && ls_key-fieldname && ` = ` &&
  52.          `'` && ls_clval-clause_value && `'`.
  53.  
  54.         " OR
  55.       ENDLOOP.
  56.       IF lines( ls_key-t_clval ) NE 0.
  57.         lv_clkey = lv_clkey && ` ) `.
  58.       ENDIF.
  59.       " AND
  60.     ENDLOOP.
  61.  
  62.  
  63.     IF iv_fldexcl EQ abap_true.
  64.       " construire la clause where
  65.       LOOP AT ls_tblmrg-t_fld INTO ls_fld.
  66.  
  67.         IF lines( ls_fld-t_clval ) NE 0 AND sy-tabix NE 1.
  68.           lv_clfld = lv_clfld && ` AND `.
  69.         ENDIF.
  70.         IF lines( ls_fld-t_clval ) NE 0.
  71.           lv_clfld = lv_clfld && ` ( `.
  72.         ENDIF.
  73.         LOOP AT ls_fld-t_clval INTO ls_clval.
  74.           IF sy-tabix NE 1.
  75.             lv_clfld = lv_clfld && ` OR `.
  76.           ENDIF.
  77.           lv_clfld = lv_clfld && ls_fld-fieldname && ` = ` &&
  78.            `'` && ls_clval-clause_value && `'`.
  79.  
  80.           " OR
  81.         ENDLOOP.
  82.         IF lines( ls_fld-t_clval ) NE 0.
  83.           lv_clfld = lv_clfld && ` ) `.
  84.         ENDIF.
  85.         " AND
  86.       ENDLOOP.
  87.     ENDIF.
  88.     IF lv_clkey IS NOT INITIAL AND lv_clfld IS NOT INITIAL.
  89.       lv_clause = lv_clkey && ` AND ` && lv_clfld.
  90.     ELSEIF lv_clkey IS INITIAL AND lv_clfld IS NOT INITIAL.
  91.       lv_clause = lv_clfld.
  92.     ELSEIF lv_clkey IS NOT INITIAL AND lv_clfld IS INITIAL.
  93.       lv_clause = lv_clkey.
  94.     ELSE.
  95.       CONTINUE.
  96.     ENDIF.
  97.  
  98.     CREATE DATA lt_tblref TYPE TABLE OF (ls_tblmrg-tabname).
  99.     ASSIGN lt_tblref->* TO <fs_tbl>.
  100.  
  101.     SELECT * FROM (ls_tblmrg-tabname) INTO TABLE <fs_tbl>
  102.       WHERE (lv_clause).
  103.  
  104.     IF lines( <fs_tbl> ) EQ 0.
  105.       " TODO protocole
  106.       CONTINUE.
  107.     ENDIF.
  108.  
  109.     ls_tbllin-tabname = ls_tblmrg-tabname.
  110.     ls_tbllin-entries = lt_tblref.
  111.     lv_index = lines( et_tbllin ).
  112.     ADD 1 TO lv_index.
  113.     INSERT ls_tbllin INTO et_tbllin INDEX lv_index.
  114.  
  115.  
  116.   ENDLOOP.
  117. ENDFORM.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement