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

Database not updating using Update Function Module

Former Member
0 Likes
3,187

Hi Experts,


I had written the Following Update Function Module to update the student marks in zstudentmarks  based on the name of student imported from main program.


FUNCTION ZF_MARKS .

*"----------------------------------------------------------------------

*"*"Update Function Module:

*"

*"*"Local Interface:

*"  IMPORTING

*"     VALUE(NAME) TYPE  CHAR30

*"----------------------------------------------------------------------

   DATA lv_id TYPE int1.

* Getting StudentID Based on name

   SELECT SINGLE stud_id

     FROM zstudentmaster

     INTO lv_id

     WHERE stud_name = name.

   wa-id = lv_id.

   wa-mark1 = 35.

   wa-mark2 = 35.

   wa-mark3 = 38.

   wa-mark4 = 36.

   wa-mark5 = 35.



*Inserting into Internal Table

   INSERT wa INTO TABLE itab.

*Updating entry in DB

   MODIFY zstudentmarks FROM TABLE itab.

   COMMIT WORK.

ENDFUNCTION.



While Debugging the record is showing in the internal table. But it is not updating in the database....



Thanx in advance...

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,779

hi pankaj,

may be record is already there and therefore it is not updating.

also for checking the problem

replace

*Updating entry in DB

   MODIFY zstudentmarks FROM TABLE itab.


with


*Updating entry in DB

   MODIFY zstudentmarks FROM wa.

8 REPLIES 8
Read only

rajkumarnarasimman
Active Contributor
0 Likes
1,779

Hi Pankaj,

Kindly check whether mark1, mark2, mark3, mark4, mark5 is not a primary key. If it is a primary key, then system wont update the value.  In debug mode check the Subrc value in modify statement.


Make sure Itab has the same structure of the Ztable 'zstudentmarks'.

Regards

Rajkumar Narasimman

Read only

0 Likes
1,779

Hello Rajkumar N,

The Internal Table and Zstudentmarks has the same structure.

And mark1, mark2, mark3, mark4, mark5 is not a primary key. The primary key is the stud_id.

Read only

former_member185613
Contributor
0 Likes
1,779

Hi,

If you have client field (field having data element MANDT) in the custom table, try to include it in the work area WA.

If there is already an entry in the table with the same primary key(s) as in work area, you will end up getting a data dump with error statement saying 'duplicate record'. Please check if that is the case here.

Hope this helps,

~Athreya

Read only

0 Likes
1,779

Hello,

There is no MANDT field in the table.

stud_id is a primary key and i want to update the existing record not inserting another record. It is not giving any error. If I am copying this code to the REPORT program than its working fine and updating the records in database.

Read only

Former Member
0 Likes
1,779

Hi ,

Have you declared your internal table like this

Data itab type table of zstudentmarks with header line.


And work area


wa like line of itab.


You can select and modify using this


select single * from zstudmarks into wa where stud_name = name.
  
   if ( sy-subrc = 0 ).
*      modify the fields you want here
          wa-mark1 = 78.
          
wa-mark2 = 79.
    

* modify table

modify  zstudmarks from wa.

     endif.


I hope this helps.



Read only

matt
Active Contributor
0 Likes
1,779

Sayeh Cyril wrote:

Hi ,

Have you declared your internal table like this

Data itab type table of zstudentmarks with header line.


And work area


wa like line of itab

First, HEADER LINES are obsolete. Second, if you have a header line, you don't need a work area defined seperately.

Read only

Former Member
0 Likes
1,779

Hi Pankaj,

Can you check that SELECT query again?

Student name is not a key field and in where clause you are using no key fields.

Now you may check for the following things,

1. when you select the Stud_ID based on name, what happens if two same name exist with different Stud_IDs?

Read only

Former Member
0 Likes
1,780

hi pankaj,

may be record is already there and therefore it is not updating.

also for checking the problem

replace

*Updating entry in DB

   MODIFY zstudentmarks FROM TABLE itab.


with


*Updating entry in DB

   MODIFY zstudentmarks FROM wa.