Skip to end of metadata
Go to start of metadata

Fetching hr data in different ways
 
There are 3 methods in fetching the data from HR database tables . 

1. Using select statements

This is the simple method for the selection of data from the hr tables(pannnn). in the HR data time is the major component , in the where condition we should include the date parameters for fetching the data .see this simple example  to understand 

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zhrtests.
TABLES: pa0002 .
DATA:BEGIN OF itab OCCURS 0,
       pernr LIKE pa0002-pernr,
       begda LIKE pa0002-begda,
       endda LIKE pa0002-endda,
       vorna LIKE pa0002-vorna,
       nachn LIKE pa0002-nachn,
     END OF itab .

SELECT-OPTIONS:s_pernr FOR pa0002-pernr ,
               s_date  FOR sy-datum.

START-OF-SELECTION .
  SELECT pernr
         begda
         endda
         vorna
         nachn
         FROM pa0002
         INTO TABLE itab
         WHERE pernr IN s_pernr
         AND begda LE s_date-high
         AND endda GE s_date-low.
  IF NOT itab[] IS INITIAL.
    LOOP AT itab .
      WRITE:/ itab-pernr,
              itab-begda,
              itab-endda,
              itab-vorna,
              itab-nachn .
    ENDLOOP.
  ENDIF.

2.using Different function modules..
 1. using function module  hr_read_infotype
 
This function module is used for fetching the data from Personnel Administration and Time management info types . This is used to get a single PERNR from the Info type .see the below example....

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT  zhrtests.
TABLES:pa0002 .
DATA: itab TYPE p0002 OCCURS 0 WITH HEADER LINE .
SELECT-OPTIONS:s_pernr FOR pa0002-pernr NO INTERVALS NO-EXTENSION,
               s_date FOR sy-datum.

START-OF-SELECTION .

  CALL FUNCTION 'HR_READ_INFOTYPE'
    EXPORTING
      tclas     = 'A'
      pernr     = s_pernr-low
      infty     = '0002'
      begda     = s_date-low
      endda     = s_date-high
    TABLES
      infty_tab = itab.
  IF NOT itab[] IS INITIAL.
    LOOP AT itab .
      WRITE:/ itab-pernr,
              itab-begda,
              itab-endda,
              itab-vorna,
              itab-nachn .
    ENDLOOP.
  ENDIF.

 2. Using  function module  rh_read_infty

 
This is used for the organization management info types. With this function module we can get the relations for the given object ( means we can get position for a pernr , organization for a position , organization for an organization....) 

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT  ygetsupervisor.
*&---------------------------------------------------------------------*
*                            database tables used
*&---------------------------------------------------------------------*
TABLES:pa0001,
       hrp1001,
       pa0002.
*&---------------------------------------------------------------------*
*                            internal tables declaration
*&---------------------------------------------------------------------*
DATA: t1001          LIKE p1001 OCCURS 0 WITH HEADER LINE,
      t1002          LIKE p1001 OCCURS 0 WITH HEADER LINE,
      t1003          LIKE p1001 OCCURS 0 WITH HEADER LINE,
      t1004          LIKE p1001 OCCURS 0 WITH HEADER LINE,
      t0001          LIKE p0001 OCCURS 0 WITH HEADER LINE,
      it_pa0002      TYPE pa0002 OCCURS 0 WITH HEADER LINE.
*&---------------------------------------------------------------------*
*                         variable declaration
*&---------------------------------------------------------------------*
DATA: v_sobid1  LIKE p1001-objid,
      v_sobid2  LIKE p1001-objid,
      v_sobid3  LIKE p1001-objid,
      v_pernr LIKE pa0002-pernr.
*&---------------------------------------------------------------------*
*                         selection screen paramters
*&---------------------------------------------------------------------*
PARAMETERS :pernr LIKE pa0001-pernr.
*&---------------------------------------------------------------------*
*                         start of selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  CALL FUNCTION 'RH_READ_INFTY'
    EXPORTING
      plvar = '01'
      otype = 'P'
      objid = pernr
      infty = '1001'
      subty = 'B008'
      begda = sy-datum
      endda = sy-datum
    TABLES
      innnn = t1001.
  SORT t1001 BY begda DESCENDING .
  READ TABLE t1001 WITH KEY objid = pernr
                            otype = 'P'
                            rsign = 'B'
                            relat = '008'
                            sclas = 'S'.
  IF sy-subrc = 0.
    v_sobid1 = t1001-sobid.
    CALL FUNCTION 'RH_READ_INFTY'
      EXPORTING
        plvar = '01'
        otype = 'S'
        objid = v_sobid1
        infty = '1001'
        subty = 'A003'
        begda = sy-datum
        endda = sy-datum
      TABLES
        innnn = t1002.
  ENDIF.
  SORT t1002 BY begda DESCENDING .
  READ TABLE t1002 WITH KEY objid = v_sobid1
                            otype = 'S'
                            rsign = 'A'
                            relat = '003'
                            sclas = 'O'.
  IF sy-subrc = 0.
    v_sobid2 = t1002-sobid.
    CALL FUNCTION 'RH_READ_INFTY'
      EXPORTING
        plvar = '01'
        otype = 'O'
        objid = v_sobid2
        infty = '1001'
        subty = 'B012'
        begda = sy-datum
        endda = sy-datum
      TABLES
        innnn = t1003.
  ENDIF.
  SORT  t1003 BY objid.
  READ TABLE t1003 WITH KEY objid = v_sobid2
                            otype = 'O'
                            rsign = 'B'
                            relat = '012'
                            sclas = 'S'.
  IF sy-subrc = 0.
    v_sobid3 = t1003-sobid.
    CALL FUNCTION 'RH_READ_INFTY'
      EXPORTING
        plvar = '01'
        otype = 'S'
        objid = v_sobid3
        infty = '1001'
        subty = 'A008'
        begda = sy-datum
        endda = sy-datum
      TABLES
        innnn = t1004.
  ENDIF.
  READ TABLE t1004 WITH KEY objid = v_sobid3
                            otype = 'S'
                            rsign = 'A'
                            relat = '008'
                            sclas = 'P'.
  IF sy-subrc = 0.
    v_pernr = t1004-sobid+0(8).
    SELECT pernr
           vorna
           nachn
           cname
           FROM pa0002
           INTO CORRESPONDING FIELDS OF TABLE it_pa0002
           WHERE pernr = v_pernr.
    SORT it_pa0002 BY pernr begda.
    READ TABLE it_pa0002 INDEX 1.
    IF sy-subrc EQ 0.
      WRITE:/ it_pa0002-pernr,it_pa0002-vorna,it_pa0002-nachn,it_pa0002-cname.
    ENDIF.
  ENDIF.

3. Using function module rh_struc_get
 
This is used to get the higher organization unit for the  given personnel no. this is easier way for getting the highest org unit for  a given pesonnel no or for a given org.unit ..

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT  zhrtests.
PARAMETERS: objid TYPE objid.
DATA:result_objec TYPE objec OCCURS 0 WITH HEADER LINE ,
     result_objec1 TYPE objec OCCURS 0 WITH HEADER LINE ,
     v_objid1 TYPE objid .
*---here we will get the organization unit for the given pesonnel number.
CALL FUNCTION 'RH_STRUC_GET'
  EXPORTING
    act_otype    = 'P'
    act_objid    = objid
    act_wegid    = 'P-S-O'               " person-position-orgunit
  TABLES
    result_objec = result_objec.
LOOP AT result_objec  WHERE otype = 'O'.

*--- loop the orgunits only to get the higher orgunits
  v_objid1  = result_objec-objid.
  CALL FUNCTION 'RH_STRUC_GET'
    EXPORTING
      act_otype    = 'O'
      act_objid    = v_objid1
      act_wegid    = 'O-O'
    TABLES
      result_objec = result_objec1.
  LOOP AT result_objec1.
  ENDLOOP.
*----this is the highest orgunit for the Personnel number
  WRITE:/ result_objec1-objid.
ENDLOOP.

3. Using logical databases(macros)

Here i am using the PNP logical database for this example  . And I am using the macros for getting the data from the databases .

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT  zhrtests.
TABLES: pernr.
INFOTYPES: 0002.

GET pernr.
*---get the first record for the pernr from the infotype
  rp_provide_from_frst p0002 space pn-begda pn-endda .
  IF pnp-sw-found EQ 1.
    WRITE: / pernr-pernr,
             pn-begda,
             pn-endda,
             p0002-vorna,
             p0002-nachn,
             p0002-gbdat.
  ENDIF.
*---get the last  record for the pernr from the infotype
  rp_provide_from_last p0002 space pn-begda pn-endda.
  IF pnp-sw-found EQ 1.
    WRITE: /  pernr-pernr,
              pn-begda,
              pn-endda,
              p0002-vorna,
              p0002-nachn,
              p0002-gbdat.
  ENDIF.