Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Innerjoin b/w these 2 tables VBAK and CDHDR ?

Former Member
0 Kudos

Hi,

How to use Innerjoin b/w these 2 tables VBAK and CDHDR for retrieving of changed sales order data ?I nned the A select stmt with INNER JOIN for changed sales data.

Thanks & Regards,

gopi.

2 REPLIES 2

Former Member
0 Kudos

Hi Gopi,

You can use the code similar to below to read the changes to Sales order from CDHDR AND CDPOS

FORM GET_CHANGES .

* Get all the changes to Sales Order as well as Deliveries. Suppress
* Non relevant change

  DATA : BEGIN OF SO_VBELN OCCURS 0 ,
       VBELN LIKE CDHDR-OBJECTID,
       END OF SO_VBELN.
  DATA : BEGIN OF DL_VBELN OCCURS 0 ,
       VBELN LIKE CDHDR-OBJECTID,
       END OF DL_VBELN.
  DATA TEMP_SO_VBELN LIKE SO_VBELN OCCURS 0 WITH HEADER LINE.
  DATA DATE_UNTIL LIKE SY-DATUM.
  DATA TIME_UNTIL LIKE SY-UZEIT .
  DATA DATE_FROM LIKE SY-DATUM.


  READ TABLE S_CHADT INDEX 1.

  LOOP AT IT_VBAP.
    AT NEW VBELN.
      MOVE IT_VBAP-VBELN TO TEMP_SO_VBELN.
      COLLECT TEMP_SO_VBELN.
    ENDAT.
  ENDLOOP.

  IF TEMP_SO_VBELN[] IS NOT INITIAL.
    SELECT OBJECTID AS VBELN FROM CDHDR
         INTO TABLE SO_VBELN
           FOR ALL ENTRIES IN TEMP_SO_VBELN
         WHERE  OBJECTCLAS EQ 'VERKBELEG'
         AND OBJECTID EQ TEMP_SO_VBELN-VBELN
         AND USERNAME IN S_CHABY
         AND UDATE IN S_CHADT.
  ENDIF.

  LOOP AT IT_LIPS INTO WA_LIPS.
    AT NEW VBELN.
      MOVE WA_LIPS-VBELN TO DL_VBELN.
      COLLECT DL_VBELN.
    ENDAT.
  ENDLOOP.

* Changes in Sales Order.
  IF NOT  SO_VBELN[] IS INITIAL.

* Get partner functions
    SELECT * INTO TABLE IT_TPART
      FROM TPART WHERE SPRAS EQ SY-LANGU.

    SELECT A~TABNAME A~FIELDNAME A~DATATYPE B~DDTEXT
       INTO CORRESPONDING FIELDS OF TABLE IT_FIELD
       FROM DD03L AS A INNER JOIN
            DD04T AS B
            ON A~ROLLNAME EQ B~ROLLNAME
            AND A~AS4LOCAL EQ B~AS4LOCAL
          WHERE ( A~TABNAME EQ 'VBAK'
                  OR A~TABNAME EQ 'VBAP'
                  OR A~TABNAME EQ 'VBEP'
                  OR A~TABNAME EQ 'VBPA'
                  OR A~TABNAME EQ 'VBUK'
                  OR A~TABNAME EQ 'VBKD' )
           AND  A~AS4LOCAL EQ 'A'
           AND A~AS4VERS EQ '0000'
           AND A~ROLLNAME NE SPACE
           AND B~DDLANGUAGE EQ SY-LANGU.

* Preapare the exclusion table
    R_EXCL_FIELDS-OPTION = 'EQ'.
    R_EXCL_FIELDS-SIGN = 'I'.

    LOOP AT IT_FIELD WHERE DATATYPE EQ 'CURR'.
      R_EXCL_FIELDS-LOW = IT_FIELD-FIELDNAME.
      APPEND R_EXCL_FIELDS.
    ENDLOOP.
    R_EXCL_FIELDS-LOW = 'ARKTX'.
    APPEND R_EXCL_FIELDS.
    R_EXCL_FIELDS-LOW = 'LGORT'.
    APPEND R_EXCL_FIELDS.
    R_EXCL_FIELDS-LOW = 'TDDAT'.
    APPEND R_EXCL_FIELDS.
    R_EXCL_FIELDS-LOW = 'LDDAT'.
    APPEND R_EXCL_FIELDS.
    R_EXCL_FIELDS-LOW = 'MBDAT'.
    APPEND R_EXCL_FIELDS.

    R_EXCL_TABL-OPTION = 'EQ'.
    R_EXCL_TABL-SIGN = 'I'.
    R_EXCL_TABL-LOW = 'KONVC'.
    APPEND R_EXCL_TABL.
    R_EXCL_TABL-LOW = 'VBKD'.
    APPEND R_EXCL_TABL.

    LOOP AT SO_VBELN.
      REFRESH ITAB_CDHDR.
      DATE_FROM = S_CHADT-LOW.
      DATE_UNTIL = S_CHADT-HIGH.
      CALL FUNCTION 'CHANGEDOCUMENT_READ_HEADERS'
        EXPORTING
          DATE_OF_CHANGE             = DATE_FROM
          OBJECTCLASS                = 'VERKBELEG'
          OBJECTID                   = SO_VBELN-VBELN
          TIME_OF_CHANGE             = TIME_UNTIL
          DATE_UNTIL                 = DATE_UNTIL
          USERNAME                   = ' '
        TABLES
          I_CDHDR                    = ITAB_CDHDR
        EXCEPTIONS
          NO_POSITION_FOUND          = 1
          WRONG_ACCESS_TO_ARCHIVE    = 2
          TIME_ZONE_CONVERSION_ERROR = 3
          OTHERS                     = 4.
      IF SY-SUBRC NE 0.
        CONTINUE.
      ENDIF.
      LOOP AT ITAB_CDHDR WHERE USERNAME IN S_CHABY.
        REFRESH IT_CDSHW.
        CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS'
          EXPORTING
            CHANGENUMBER            = ITAB_CDHDR-CHANGENR
          TABLES
            EDITPOS                 = IT_CDSHW
          EXCEPTIONS
            NO_POSITION_FOUND       = 1
            WRONG_ACCESS_TO_ARCHIVE = 2
            OTHERS                  = 3.
        IF SY-SUBRC EQ 0.
          PERFORM GET_ACTION .
        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDIF.

* Changes in Delivery
  IF NOT DL_VBELN[] IS INITIAL.
    REFRESH : IT_CDHDR , IT_CDPOS.
    SELECT  OBJECTCLAS OBJECTID CHANGENR USERNAME UDATE
           FROM CDHDR INTO CORRESPONDING FIELDS OF TABLE IT_CDHDR
         FOR ALL ENTRIES IN DL_VBELN
       WHERE  OBJECTCLAS EQ 'LIEFERUNG'
       AND OBJECTID EQ DL_VBELN-VBELN
       AND USERNAME IN S_CHABY
       AND UDATE IN S_CHADT
       AND CHANGE_IND EQ 'U'.
    LOOP AT IT_CDHDR.
      CONCATENATE SY-MANDT IT_CDHDR-OBJECTID
       INTO IT_CDHDR-TABKEY .
      CONDENSE  IT_CDHDR-TABKEY.
      MODIFY IT_CDHDR.
    ENDLOOP.

    IF NOT IT_CDHDR[] IS INITIAL.
      SELECT  * INTO TABLE IT_CDPOS FROM CDPOS
           FOR ALL ENTRIES IN IT_CDHDR
         WHERE  OBJECTCLAS EQ IT_CDHDR-OBJECTCLAS
         AND OBJECTID EQ IT_CDHDR-OBJECTID
         AND CHANGENR EQ IT_CDHDR-CHANGENR
         AND TABNAME EQ 'LIKP'
         AND TABKEY EQ IT_CDHDR-TABKEY
         AND FNAME IN ('WADAT','LFDAT','ROUTE','VSBED')
         AND CHNGIND EQ 'U' .

      LOOP AT IT_CDPOS .
        CLEAR  IT_DL_CHG-FLAG_RED.
        CASE IT_CDPOS-FNAME.
          WHEN 'WADAT'.
            MOVE 'Goods issue date changed' TO IT_DL_CHG-ACTION.
          WHEN 'LFDAT'.
            MOVE 'Delivery date changed' TO IT_DL_CHG-ACTION.
            MOVE 'X'  TO IT_DL_CHG-FLAG_RED.
          WHEN 'ROUTE'.
            MOVE 'Route Changed' TO IT_DL_CHG-ACTION.
          WHEN 'VSBED'.
            MOVE 'Shipping Condition Changed' TO IT_DL_CHG-ACTION.
          WHEN OTHERS.
        ENDCASE.

        AT NEW CHANGENR .
          READ TABLE IT_CDHDR WITH KEY OBJECTCLAS = IT_CDPOS-OBJECTCLAS
                                       OBJECTID = IT_CDPOS-OBJECTID
                                       CHANGENR = IT_CDPOS-CHANGENR
                                       BINARY SEARCH.
          MOVE IT_CDHDR-USERNAME TO  IT_DL_CHG-ERNAM.
          WRITE IT_CDHDR-UDATE TO  IT_DL_CHG-ERDAT.
        ENDAT.
        IT_DL_CHG-COUNTER = IT_DL_CHG-COUNTER + 1.
        MOVE IT_CDPOS-OBJECTID TO IT_DL_CHG-VBELN.
        MOVE IT_CDPOS-VALUE_OLD TO IT_DL_CHG-OLD_VAL.
        MOVE IT_CDPOS-VALUE_NEW TO IT_DL_CHG-NEW_VAL.
        INSERT TABLE IT_DL_CHG.
      ENDLOOP.
    ENDIF.
  ENDIF.
ENDFORM.                    " GET_CHANGES

Cheers

VJ

former_member186741
Active Contributor
0 Kudos

something like:

select * from vbak

join cdhdr on objectclas = 'VERKBELEG'

and objectid = vbak~vbeln

into corresponding fields of table itab

where .............