ABAP Dinamik SQL programlama ile Veri Karşılaştırma kodu kullanımı örneği (ABAP Dynamic SQL programming & Data Comparing code usage example)

Bu örnek kodda ABAP içinde dinamik SQL programlama (Dynamic SQL Programming) ve field-symbols kullanımları için örnek kod vardır.
Program özellikle yeni açılan ana verilerde (master data) ortaya çıkan problemlerde hataya sebep olan farklılıkları bulabilmek için yazılmıştır.

Ekranda girilecek maksimum 5 adet tabloyu, verilen alan adlarında (field name) için değer1 (value1) ve değer2 (value2) değerleri için ana veriyi okuyup farklı olan alanları listelemektedir.

Programın amacı aynı tablo içinden verilen anahtarlara (keys) göre 2 satır okuyup aralarındaki farklılıkları bulmaktır.

Burada tablo kısmına karşılaştırma istenen tablo/tablolar girilir.
Alan (Field) kısmına ise seçilen tablo/tabloların anahtar alanları (key fields) ve karşılaştırma istenen değerler girilir.

Örnekte mara, marc, mvke tabloları 2 değer için karşılatırılıp farklılıklar listelenmiştir.

Selection Screen

Selection Screen

dymanicSQLresult

——————————————————————————–
02.01.14 TRD Program ZFIASB_DYNAMIC_SQL Page 1
——————————————————————————–

1 *&———————————————————————*
2 *&———————————————————————*
3 *&———————————————————————*
4 *& Report ZFIASB_DATACOMPARE
5 *&———————————————————————*
6 *& Sipariş Kontrol
7 *&
8 *&———————————————————————*
9
10 REPORT zfiasb_dynamic_sql .
11 TYPE-POOLS : slis, esp1.
12
13 ******************************************
14 DATA : gt_header TYPE slis_t_listheader WITH HEADER LINE.
15 DATA : gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
16 DATA : i_program_name LIKE sy-repid.
17 DATA : ls_layout TYPE slis_layout_alv.
18 *****************************************
19 DATA : i_message_tab TYPE esp1_message_wa_type OCCURS 0,
20 wa_message_tab TYPE esp1_message_wa_type.
21 DATA wa_alladr LIKE zfi_alladrf.
22
23 DATA lt_dd03l LIKE TABLE OF dd03l WITH HEADER LINE.
24 DATA : BEGIN OF lt_itab2 OCCURS 0,
25 tabname LIKE dd03l-tabname,
26 fieldname LIKE dd03l-fieldname,
27 ddtext LIKE dd03m-ddtext,
28 value1(30),
29 value2(30),
30 fark(1),
31 END OF lt_itab2.
32 DATA ls_itab LIKE LINE OF lt_itab2.
33 PARAMETERS : ptabnm01 TYPE tabname.
34 PARAMETERS : ptabnm02 TYPE tabname.
35 PARAMETERS : ptabnm03 TYPE tabname.
36 PARAMETERS : ptabnm04 TYPE tabname.
37 PARAMETERS : ptabnm05 TYPE tabname.
38
39 SELECTION-SCREEN BEGIN OF LINE.
40 SELECTION-SCREEN COMMENT (33) text-c40.
41 SELECTION-SCREEN COMMENT (33) text-c4a.
42 SELECTION-SCREEN COMMENT (33) text-c4b.
43 SELECTION-SCREEN COMMENT (33) text-c4c.
44 SELECTION-SCREEN END OF LINE.
45
46 SELECTION-SCREEN BEGIN OF LINE.
47 SELECTION-SCREEN COMMENT (33) text-c41.
48 PARAMETERS : pfld01 TYPE fieldname.
49 PARAMETERS : pvlp01 TYPE char30.
50 PARAMETERS : pvln01 TYPE char30.
51 SELECTION-SCREEN END OF LINE.
52
53 SELECTION-SCREEN BEGIN OF LINE.
54 SELECTION-SCREEN COMMENT (33) text-c42.
55 PARAMETERS : pfld02 TYPE fieldname.
56 PARAMETERS : pvlp02 TYPE char30.
57 PARAMETERS : pvln02 TYPE char30.
58 SELECTION-SCREEN END OF LINE.
59
60 SELECTION-SCREEN BEGIN OF LINE.
61 SELECTION-SCREEN COMMENT (33) text-c43.
——————————————————————————–
02.01.14 TRD Program ZFIASB_DYNAMIC_SQL Page 2
——————————————————————————–

62 PARAMETERS : pfld03 TYPE fieldname.
63 PARAMETERS : pvlp03 TYPE char30.
64 PARAMETERS : pvln03 TYPE char30.
65 SELECTION-SCREEN END OF LINE.
66
67 SELECTION-SCREEN BEGIN OF LINE.
68 SELECTION-SCREEN COMMENT (33) text-c44.
69 PARAMETERS : pfld04 TYPE fieldname.
70 PARAMETERS : pvlp04 TYPE char30.
71 PARAMETERS : pvln04 TYPE char30.
72 SELECTION-SCREEN END OF LINE.
73
74 SELECTION-SCREEN BEGIN OF LINE.
75 SELECTION-SCREEN COMMENT (33) text-c45.
76 PARAMETERS : pfld05 TYPE fieldname.
77 PARAMETERS : pvlp05 TYPE char30.
78 PARAMETERS : pvln05 TYPE char30.
79 SELECTION-SCREEN END OF LINE.
80
81 INITIALIZATION.
82
83 START-OF-SELECTION.
84 PERFORM get_data USING ptabnm01.
85 PERFORM get_data USING ptabnm02.
86 PERFORM get_data USING ptabnm03.
87 PERFORM get_data USING ptabnm04.
88 PERFORM get_data USING ptabnm05.
89 PERFORM build_fcat.
90 PERFORM display_data.
91
92 *&———————————————————————*
93 *& Form SON_SAS_HAREKETLERI
94 *&———————————————————————*
95 * text
96 *———————————————————————-*
97 * –> p1 text
98 * <– p2 text
99 *———————————————————————-*
100
101 FORM get_data USING ptabname.
102 FIELD-SYMBOLS: <fs> TYPE ANY.
103 FIELD-SYMBOLS: <fsp> TYPE ANY.
104 FIELD-SYMBOLS: <fsn> TYPE ANY.
105 DATA fldname(30).
106
107 DATA qq TYPE n LENGTH 2.
108
109 FIELD-SYMBOLS: <row1> TYPE ANY,
110 <component1> TYPE ANY.
111 FIELD-SYMBOLS: <row2> TYPE ANY,
112 <component2> TYPE ANY.
113 DATA: dref TYPE REF TO data.
114 DATA: dref2 TYPE REF TO data.
115
116 DATA: where_tab1 TYPE TABLE OF edpline,
117 where_tab2 TYPE TABLE OF edpline,
118 source_line TYPE edpline.
119
120 SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_dd03l
121 FROM dd03l
122 WHERE tabname = ptabname
——————————————————————————–
02.01.14 TRD Program ZFIASB_DYNAMIC_SQL Page 3
——————————————————————————–

123 AND comptype = ‘E’.
124 CHECK lt_dd03l[] IS NOT INITIAL.
125 CHECK ptabname IS NOT INITIAL.
126 CREATE DATA dref TYPE (ptabname).
127 ASSIGN dref->* TO <row1>.
128 CREATE DATA dref2 TYPE (ptabname).
129 ASSIGN dref2->* TO <row2>.
130 DO 5 TIMES.
131 qq = sy-index.
132 CONCATENATE ‘PFLD’ qq INTO fldname.
133 ASSIGN (fldname) TO <fs>.
134 CHECK <fs> IS NOT INITIAL.
135 CLEAR lt_dd03l.
136 READ TABLE lt_dd03l WITH KEY tabname = ptabname
137 fieldname = <fs>
138 keyflag = ‘X’.
139 CHECK sy-subrc = 0.
140 CONCATENATE ‘PVLP’ qq INTO fldname.
141 ASSIGN (fldname) TO <fsp>.
142 CONCATENATE ‘PVLN’ qq INTO fldname.
143 ASSIGN (fldname) TO <fsn>.
144
145 CONCATENATE <fs> ‘ = ”’ <fsp>
146 ”” INTO source_line.
147 IF where_tab1[] IS NOT INITIAL.
148 CONCATENATE ‘AND’ source_line INTO source_line SEPARATED BY space.
149 ENDIF.
150 APPEND source_line TO where_tab1.
151
152 CONCATENATE <fs> ‘ = ”’ <fsn>
153 ”” INTO source_line.
154 IF where_tab2[] IS NOT INITIAL.
155 CONCATENATE ‘AND’ source_line INTO source_line SEPARATED BY space.
156 ENDIF.
157 APPEND source_line TO where_tab2.
158 ENDDO.
159 SELECT SINGLE *
160 FROM (ptabname)
161 INTO <row1>
162 WHERE (where_tab1).
163 SELECT SINGLE *
164 FROM (ptabname)
165 INTO <row2>
166 WHERE (where_tab2).
167 ** mara
168
169 LOOP AT lt_dd03l.
170 ASSIGN COMPONENT lt_dd03l-fieldname OF STRUCTURE <row1>
171 TO <component1>.
172 ASSIGN COMPONENT lt_dd03l-fieldname OF STRUCTURE <row2>
173 TO <component2>.
174
175 IF <component1> <> <component2>.
176 CLEAR lt_itab2.
177 lt_itab2-tabname = lt_dd03l-tabname.
178 lt_itab2-fieldname = lt_dd03l-fieldname.
179 lt_itab2-value1 = <component1>.
180 lt_itab2-value2 = <component2>.
181 PERFORM degeroku USING lt_itab2-tabname lt_itab2-fieldname
182 CHANGING lt_itab2-ddtext.
183 APPEND lt_itab2.
——————————————————————————–
02.01.14 TRD Program ZFIASB_DYNAMIC_SQL Page 4
——————————————————————————–

184 ENDIF.
185 ENDLOOP.
186
187 ENDFORM. " SON_SAS_HAREKETLERI
188 *&———————————————————————*
189 *& Form BUILD_FCAT
190 *&———————————————————————*
191 FORM build_fcat .
192 DEFINE macro_fill_fieldcat.
193 gt_fieldcat-seltext_l = &1.
194 gt_fieldcat-no_zero = &2.
195 gt_fieldcat-do_sum = &3.
196 if not gt_fieldcat-seltext_l is initial.
197 move : gt_fieldcat-seltext_l to gt_fieldcat-seltext_m,
198 gt_fieldcat-seltext_l to gt_fieldcat-seltext_s,
199 gt_fieldcat-seltext_l to gt_fieldcat-reptext_ddic.
200 endif.
201 modify gt_fieldcat index sy-tabix.
202 END-OF-DEFINITION.
203
204 REFRESH gt_fieldcat.
205 i_program_name = sy-repid.
206 CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE’
207 EXPORTING
208 i_program_name = i_program_name
209 i_internal_tabname = ‘LT_ITAB2’
210 i_inclname = i_program_name
211 i_bypassing_buffer = ‘X’
212 CHANGING
213 ct_fieldcat = gt_fieldcat[]
214 EXCEPTIONS
215 inconsistent_interface = 1
216 program_error = 2
217 OTHERS = 3.
218 LOOP AT gt_fieldcat.
219 CASE gt_fieldcat-fieldname.
220 WHEN ‘VALUE1’. "Desc No_zero do_sum no_out
221 macro_fill_fieldcat ‘Value1’ ‘ ‘ ‘ ‘ .
222 WHEN ‘VALUE2’. "Desc No_zero do_sum no_out
223 macro_fill_fieldcat ‘Value2’ ‘ ‘ ‘ ‘ .
224 ENDCASE.
225 ENDLOOP.
226 ls_layout-info_fieldname = ‘CLR’.
227 ls_layout-colwidth_optimize = ‘X’.
228 ls_layout-zebra = ‘X’.
229 ls_layout-numc_sum = ‘X’.
230 ls_layout-totals_text = ‘?’.
231 ls_layout-subtotals_text = ‘??’.
232 ls_layout-cell_merge = ‘N’.
233
234 ENDFORM. " BUILD_FCAT
235
236 *&———————————————————————*
237 *& Form DISPLAY_DATA
238 *&———————————————————————*
239 FORM display_data .
240 PERFORM build_header.
241 CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
242 EXPORTING
243 i_callback_program = sy-repid
244 is_layout = ls_layout
——————————————————————————–
02.01.14 TRD Program ZFIASB_DYNAMIC_SQL Page 5
——————————————————————————–

245 i_callback_top_of_page = ‘TOP_OF_PAGE’
246 i_callback_user_command = ‘USER_COMMAND’
247 * i_callback_pf_status_set = ‘SET_PF_STATUS’
248 it_fieldcat = gt_fieldcat[]
249 i_save = ‘A’
250 TABLES
251 t_outtab = lt_itab2[]
252 EXCEPTIONS
253 program_error = 1
254 OTHERS = 2.
255
256 IF sy-subrc <> 0.
257 MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
258 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
259 ENDIF.
260
261 ENDFORM. " DISPLAY_DATA
262 *&———————————————————————*
263 *& Form BUILD_HEADER
264 *&———————————————————————*
265 FORM build_header .
266 DATA : baslik(120),baslik2(120).
267 CLEAR baslik.
268 CLEAR baslik2.
269 REFRESH gt_header.
270 baslik = ‘konf’..
271 gt_header-typ = ‘S’.
272 gt_header-info = baslik.
273 APPEND gt_header.
274
275
276 ENDFORM. " BUILD_HEADER
277 *———————————————————————*
278 * Form top_of_page *
279 *———————————————————————*
280 FORM top_of_page.
281 CALL FUNCTION ‘REUSE_ALV_COMMENTARY_WRITE’
282 EXPORTING
283 it_list_commentary = gt_header[].
284 ENDFORM. " top_of_page
285
286 *&——————————————————————–*
287 *& Form user_command
288 *&——————————————————————–*
289 FORM user_command USING r_ucomm LIKE sy-ucomm
290 rs_selfield TYPE slis_selfield.
291 READ TABLE lt_itab2 INDEX rs_selfield-tabindex.
292 CASE r_ucomm.
293 WHEN ‘&IC1’.
294 * SET PARAMETER ID ‘AUN’ FIELD lt_itab-MATNR.
295 * CALL TRANSACTION ‘VA03’ AND SKIP FIRST SCREEN.
296
297 ENDCASE.
298 IF sy-ucomm = ‘&F03’ OR sy-ucomm = ‘&F15’ OR sy-ucomm = ‘&F12’.
299 rs_selfield-exit = ‘X’.
300 ENDIF.
301 ENDFORM. "user_command
302 *——————————————————————–*
303 * FORM set_pf_status *
304 *——————————————————————–*
305 FORM set_pf_status USING rt_extab TYPE slis_t_extab.
——————————————————————————–
02.01.14 TRD Program ZFIASB_DYNAMIC_SQL Page 6
——————————————————————————–

306 SET PF-STATUS ‘LISTE’ EXCLUDING rt_extab.
307 ENDFORM. "set_pf_status
308 *&———————————————————————*
309 *& Form DEGEROKU
310 *&———————————————————————*
311 * text
312 *———————————————————————-*
313 * –>P_LT_ITAB2_TABNAME text
314 * –>P_LT_ITAB2_FIELDNAME text
315 * <–P_LT_ITAB2_DDTEXT text
316 *———————————————————————-*
317 FORM degeroku USING ptab
318 pfld
319 CHANGING ptext.
320 SELECT SINGLE ddtext INTO ptext
321 FROM dd03m
322 WHERE tabname = ptab
323 AND fieldname = pfld
324 AND ddlanguage = sy-langu.
325 ENDFORM. " DEGEROKU

Share on Facebook0Tweet about this on TwitterShare on Google+0Share on StumbleUpon0Share on Reddit0

Leave a Reply


*