Tuesday, June 11, 2013

SAP ABAP UPDATE SET WHERE How to change any table data.



Hi Folks,
well this is quite trivial but it might be interesting for many people.

For example you might want to change some infotype data for some people, but se16n or se16 don't let you do that even if you are using the infamous "/h" method (Btw Anybody interested in knowing how that works?)

You can use this little code snipplet:

REPORT  z_upd.
TABLES pa0001.

SELECT-OPTIONS: p_pernr FOR pa0001-pernr.
PARAMETERS:     p_setexp TYPE char100 DEFAULT 'BEGDA = ''20060101'''.
PARAMETERS:     p_table TYPE char20 DEFAULT 'PA0008'.
PARAMETERS:     p_where TYPE char200
                DEFAULT 'pernr IN p_pernr AND ENDDA = ''99991231'''.
PARAMETERS: p_test type boole_d default 'X'.

IF sy-uname NE 'uneuser' AND
   sy-uname NE 'anotheruser'.

  WRITE:/ 'user not authorized.'.
  LEAVE PROGRAM.

ENDIF.


UPDATE (p_table) SET (p_setexp) WHERE (p_where).

WRITE:/ 'Records updated ', sy-dbcnt.

if p_test = 'X'.
 rollback work.
 write: / 'Test mode. i do a rollback'.
endif.

The thing is really simple. Infotype reside in the "PAxxxx" tables. Your can find the table corresponding to each infotype in table V_T582A.
To change any infotype data just change the corresponding field in tha PA table.
For example to change the surname of the employee you just need to change the pa0002-nachn field.

To change just one field you can use the nice update... set... where statement in abap.


You can then run the program as follows.












When run it executes the statement
update pa0002 set nachn = 'NEWNAME' where PERNR IN P_PERNR AND ENDDA = '99991231'

The result is that the surname is changed on the last record of IT0002 for pernr 128912.

If you enter a selection that causes a change to many records then many records will be updated.
So be very very carefull!
Again do this at home! and NOT AT WORK!! :-) specially NOT on your productions systems!