Application Development and Automation 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: 
Read only

Update database table

Former Member
0 Likes
763

Hi guys,

I want to update a database table with the below code:

SELECT SINGLE *

FROM z_pay

WHERE period = p_period

IF sy-subrc = 0.

zft_pay-stat = '2'.

ENDIF.

UPDATE z_pay.

is this code ok or not?

1 ACCEPTED SOLUTION
Read only

vinod_vemuru2
Active Contributor
0 Likes
732

Hi Mark,

It seems some problem for me. Is the field *period is the only key field of ur ztable. When ever we update data base its better to select and update based on all key fields.

If u want to modify all the records for which period is equal to p_period then its fine.


DATA: wa_z_pay TYPE z_pay.
*CLEAR z_pay*
SELECT SINGLE *
FROM z_pay
INTO wa_z_pay      "Use explicit work areas
WHERE period = p_period

--IF sy-subrc = 0.--
--zft_pay-stat = '2'. -- 
 "Here u r getting data into zft_pay but  updating with header 
"line z_pay. So it will nor work.
--ENDIF.--
*CHECK sy-subrc IS INITIAL.*
wa_z_pay-stat = '2'.
UPDATE z_pay FROM wa_z_pay.

Thanks,

Vinod.

UPDATE z_pay.

6 REPLIES 6
Read only

Former Member
0 Likes
732

Mark,

SELECT SINGLE *

FROM z_pay

WHERE period = p_period

IF sy-subrc = 0.

zft_pay-stat = '2'.

UPDATE z_pay where period = p_period.

ENDIF.

This is the right code.

please mark all good ans.

Amit.

Read only

mvoros
Active Contributor
0 Likes
732

Hi,

it looks OK. Probably you should put update statement inside of the if statement. Also using tables with header line is obsolete. You should explicitly define your working area.

Read only

Former Member
0 Likes
732

It's OK .. But what RU updating to the table ...

SELECT SINGLE *

FROM z_pay

WHERE period = p_period

IF sy-subrc = 0.

zft_pay-stat = '2'. <-- is it z_pay-stat or zft_pay-stat

UPDATE z_pay.

ENDIF.

Read only

Former Member
0 Likes
732

Hi,

UPDATE z_pay

SET zft_pay-stat = '2'.

WHERE period = p_period.

IF sy-subrc EQ 0.

COMMIT WORK.

....

ELSE.

....

ROLLBACK WORK.

ENDIF.

Check if this serves your purpose.

<REMOVED BY MODERATOR>

Thanks,

Dhanashri.

Edited by: Dhanashri Pawar on Jun 12, 2008 2:33 PM

Edited by: Dhanashri Pawar on Jun 12, 2008 2:34 PM

Edited by: Alvaro Tejada Galindo on Jun 12, 2008 2:10 PM

Read only

Former Member
0 Likes
732

Hi,

Yes u can update table using UPDATE and SET statement but before updating a database table u need to lock it using

Function module ENQUEUE_TABLE_E

and dequeue after updation by DEQUEUE_TABLE_E by passing table name.

Read only

vinod_vemuru2
Active Contributor
0 Likes
733

Hi Mark,

It seems some problem for me. Is the field *period is the only key field of ur ztable. When ever we update data base its better to select and update based on all key fields.

If u want to modify all the records for which period is equal to p_period then its fine.


DATA: wa_z_pay TYPE z_pay.
*CLEAR z_pay*
SELECT SINGLE *
FROM z_pay
INTO wa_z_pay      "Use explicit work areas
WHERE period = p_period

--IF sy-subrc = 0.--
--zft_pay-stat = '2'. -- 
 "Here u r getting data into zft_pay but  updating with header 
"line z_pay. So it will nor work.
--ENDIF.--
*CHECK sy-subrc IS INITIAL.*
wa_z_pay-stat = '2'.
UPDATE z_pay FROM wa_z_pay.

Thanks,

Vinod.

UPDATE z_pay.