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

Сompare any two records of a given table



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP Dictionary
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Wed Sep 05, 2007 1:12 pm    Post subject: Сompare any two records of a given table Reply with quote

Author: Chandramouli Subburathinam

Description: Utility program to compare any two records of a given table. This program uses fm 'RFC_READ_TABLE'. Code works best for tables whose line size is less than 512. For tables with line size more than 512, additional parameter containing the fieldnames should be passed to fm RFC_READ_TABLE.

This program also demonstrates the use of 'READ LINE..'.

Code:
REPORT zutil_compare_record LINE-SIZE 250 NO STANDARD PAGE HEADING.
 
* input table name
PARAMETERS: tabname LIKE dd02vv-tabname.
 
* data structures
* fieldname of the table
DATA: f_nametab LIKE dntab OCCURS 0 WITH HEADER LINE.
 
* comparison table
DATA: BEGIN OF comparetab OCCURS 0,
       fieldname LIKE f_nametab-fieldname,
       fieldtext LIKE f_nametab-fieldtext,
       keyflag LIKE f_nametab-keyflag,
       ddlen LIKE f_nametab-ddlen,
       field1(50),
       field2(50),
      END OF comparetab.
* list line count
DATA: lincnt TYPE i.
FIELD-SYMBOLS: <fs> TYPE ANY.
DATA: fieldtext(50).
DATA: querytext(50).
 
* key field name
DATA: BEGIN OF keyftab OCCURS 0,
        fieldname LIKE dntab-fieldname,
        fieldval1(50),
        fieldval2(50),
      END OF keyftab.
DATA: keyn TYPE i.
 
* table structure for FM
DATA: querytab LIKE rfc_db_opt OCCURS 0 WITH HEADER LINE.
DATA: fieldtab LIKE rfc_db_fld OCCURS 0 WITH HEADER LINE.
DATA: valuetab LIKE tab512 OCCURS 0 WITH HEADER LINE.
 
*
 
START-OF-SELECTION.
 
* get the structure of the table
CALL FUNCTION 'NAMETAB_GET'
     EXPORTING
          langu               = sy-langu
          only                = 'T'
          tabname             = tabname
     TABLES
          nametab             = f_nametab
     EXCEPTIONS
          internal_error      = 1
          table_has_no_fields = 2
          table_not_activ     = 3
          no_texts_found      = 4
          OTHERS              = 5.
IF sy-subrc <> 0.
ENDIF.
 
 
LOOP AT f_nametab.
  MOVE-CORRESPONDING f_nametab TO comparetab.
  APPEND comparetab.
  CLEAR: comparetab.
* identify key field
  IF f_nametab-keyflag = 'X'.
    keyftab-fieldname = f_nametab-fieldname.
    APPEND keyftab.
    CLEAR: keyftab.
  ENDIF.
ENDLOOP.
 
DESCRIBE TABLE keyftab LINES keyn.
 
* create GUI status with exit button and 'COMP' button
SET PF-STATUS 'COMP'.
 
* display the contents
PERFORM write.
 
END-OF-SELECTION.
AT USER-COMMAND.
  sy-lsind = 0.
  REFRESH: querytab, valuetab, fieldtab.
  CLEAR: querytab, valuetab, fieldtab.

  IF sy-ucomm = 'COMP'.
* get the value entered by the user in key fields to respective column of
* keyfieldtable
    DO lincnt TIMES.
      READ LINE sy-index FIELD VALUE
                  comparetab-fieldname
                  comparetab-field1
                  comparetab-field2.
      MODIFY comparetab TRANSPORTING field1 field2
                     WHERE fieldname = comparetab-fieldname.
      READ TABLE keyftab WITH KEY fieldname = comparetab-fieldname.
      IF sy-subrc = 0.
        keyftab-fieldval1 = comparetab-field1.
        keyftab-fieldval2 = comparetab-field2.
        MODIFY keyftab TRANSPORTING fieldval1 fieldval2
                            WHERE fieldname = keyftab-fieldname.
      ENDIF.
    ENDDO.
 
 * first row
    PERFORM populate_values USING '1'.

 * second row
    PERFORM populate_values USING '2'.
    PERFORM write.
 
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  populate_values
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      \-->P_0290   text
*----------------------------------------------------------------------*
FORM populate_values USING    ind.
  REFRESH: querytab, valuetab, fieldtab.
  CLEAR: querytab, valuetab, fieldtab.
  LOOP AT keyftab.
    CONCATENATE 'KEYFTAB-FIELDVAL' ind INTO fieldtext.
    ASSIGN (fieldtext) TO <fs>.
    CONCATENATE '''' <fs> '''' INTO querytext.
    IF sy-tabix NE keyn.
      CONCATENATE querytab-text
                  keyftab-fieldname
                  '='
                  querytext
                  'and'
                  INTO querytab-text SEPARATED BY space.
      APPEND querytab.
      CLEAR querytab.
    ELSE.
      CONCATENATE querytab-text
            keyftab-fieldname
            '='
            querytext
            INTO querytab-text SEPARATED BY space.
      APPEND querytab.
      CLEAR querytab.
    ENDIF.
  ENDLOOP.
 
* query the given table with the key fields
  CALL FUNCTION 'RFC_READ_TABLE'
       EXPORTING
            query_table          = tabname
       TABLES
            options              = querytab
            fields               = fieldtab
            data                 = valuetab
       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.
  ELSE.
    READ TABLE valuetab INDEX 1.
    LOOP AT fieldtab.
* move the selected contents to respective fields of comparetab.
      LOOP AT comparetab WHERE fieldname = fieldtab-fieldname.
        CONCATENATE 'COMPARETAB-FIELD' ind INTO fieldtext.
        ASSIGN (fieldtext) TO <fs>.
        <fs> = valuetab+fieldtab-offset(fieldtab-length).
        MODIFY comparetab.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
ENDFORM.                    " populate_values
*&---------------------------------------------------------------------*
*&      Form  WRITE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM write.
  lincnt = 0.
  LOOP AT comparetab.
    IF comparetab-keyflag = 'X'.
      WRITE:/1 SY-VLINE,
* input enable for the user to enter values in key fields.
            (20) comparetab-fieldname,SY-VLINE,
            (50) comparetab-field1 INPUT ON,SY-VLINE,
            (50) comparetab-field2 INPUT ON,SY-VLINE.
    ELSE.
      IF comparetab-field1 NE comparetab-field2.
* if values are different format color red
        FORMAT COLOR = 6 INVERSE OFF INTENSIFIED OFF.
      ELSE.
* if values match show color green
        FORMAT COLOR = 5 INVERSE OFF INTENSIFIED OFF.
      ENDIF.
      WRITE:/1 SY-VLINE,
            (20) comparetab-fieldname,SY-VLINE,
            (50) comparetab-field1,SY-VLINE,
            (50) comparetab-field2,SY-VLINE.
    ENDIF.
    lincnt = lincnt + 1.
  ENDLOOP.
ENDFORM.                    " WRITE
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 -> ABAP Dictionary 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.