HR--Copy of 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 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....

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....)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 ..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 .

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.

Source link:http://wiki.sdn.sap.com/wiki/display/Snippets/Copy+of+fetching+hr+data+in+different+ways

你可能感兴趣的:(fetch)