Skip to Content
author's profile photo Former Member
Former Member

Code Dilemma

Hello everyone,

In ABAP, how do I check to see if a field is null and if it is then fill it in with some value. I have the following statement however, I am pretty sure this not correct because it seems to overwrite all my dates with current system date. So If this code runs today it will put in system date for all the records coming in but if I run this code tomorrow it will replace the date with tomorrow's date which is useless.

Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.

Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.

if sy-subrc <> 0 or lt_profit_Ctr-CALDAY IS INITIAL.

RESULT = SY-DATUM.

ENDIF

If a record for a profit center is there but the date is not there it is supposed to put in a current system date for that record but if a profit center is there with a date it MUST not do anything and move on and if profit center is not there then add it with a date.

Please let me know your ideas,

Syed.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

17 Answers

  • Best Answer
    Posted on Dec 18, 2008 at 01:49 PM

    the problem is in this line:

    if sy-subrc 0 or lt_profit_Ctr-CALDAY IS INITIAL.

    the above will be always true, because if something is read from the table than sy-subrc will be null, if nothing is selected, than the field will remain initial.

    Replace the or with and!

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 01:50 PM

    Hello, The code is still not working. Here is what I tried to do:

    I ran the following code yesterday and it put 12/18/2008 for all records with 0CALDATE NULL which was right but then I tried to run it today again and it turned all the records that had 12/18/2008 to 12/19/2008 (Today's date).

    Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.

    Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.

    if sy-subrc eq 0 AND lt_profit_Ctr-CALDAY IS INITIAL.

    RESULT = SY-DATUM.

    EndIF.

    *****************Then I tried the following******************************

    and the following code made all dates to NULL

    Select * from /BI0/MCOSTCENTER into table lt_COSTCENTER.

    Read table lt_COSTCENTER with key COSTCENTER = /BI0/MCOSTCENTER.

    if sy-subrc <> 0 AND lt_COSTCENTER-CALDAY IS INITIAL.

    RESULT = SY-DATUM.

    EndIF.

    Any other suggestions?

    Syed.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      >On my last day of SDN,

      >The moderator said to me:

      >Twelve abuse reports...

      >Eleven years old,

      >Ten points gamed,

      >Nine people lied to,

      >Eight mails wasted,

      >Seven flame wars,

      >Six angry people,

      >Five rejected posts,

      >Four locked threads,

      >Three fake ID's,

      >Two minutes to go,

      >And a Guest without a user ID.

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 04:28 PM

    Kartik,

    Can you give me the data declaration for this??? not too familiar with work areas.

    ThANKS,

    Syed.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      declare work area like this

      data: wa_profit_ctr LIKE LINE OF lt_profit_Ctr.

      and for the rest of code i'm not sure u cud use read table or u need to use loop at.

      
      Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.
       
      loop at lt_profit_ctr into wa_profit_Ctr.
       
      if sy-subrc eq 0 AND wa_profit_Ctr-CALDAY IS INITIAL.
       
      wa_profit_Ctr-CALDAY = SY-DATUM.  "Use work area of type lt_profit_ctr
      update /BI0/MProfit_Ctr with wa transporting calday.
      EndIF.
      endloop.
      

      or if u want to use read table

      then use like this

      Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.
       
      Read table lt_profit_Ctr into wa_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.
       
      if sy-subrc eq 0 AND lt_profit_Ctr-CALDAY IS INITIAL.
       
      wa_profit_Ctr-CALDAY = SY-DATUM.  "Use work area of type lt_profit_ctr
      update /BI0/MProfit_Ctr with wa transporting calday.
      EndIF.
      

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 08:11 PM

    Kartik,

    Its not able to interpret 'WITH' in the following line:

    "update /BI0/MProfit_Ctr with wa transporting calday."

    Any suggestions?

    Thanks,

    Syed

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      sorry actually the statement goes like this

       update /BI0/MProfit_Ctr with wa_profit_Ctr transporting calday.
      

      what this statement will do is that when calday is initial for the particular record u have retrieved it will assign the system date to the work area of the retrieved record but then u need to update ur database table back with this data so we use update statement.

      but again i'm not sure why are u using read instead of loop at if only one record is getting retrieved use select single.

      and

      this is the code u posted above

      Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.
      Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.
      if sy-subrc 0 or lt_profit_Ctr-CALDAY IS INITIAL.
      RESULT = SY-DATUM.
      ENDIF
      

      i have some doubts in this code, please clarify those

      Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.

      whats the thing i have marked in red, i see that its the same as ur table name what condition u r trying to check in read table.

      i think it shud go like this.

      Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr

      where PROFIT_CTR = <some variable having ur profit_ctr>

      now loop at and rest of the code is same as above discussion

      if u r sure that only one record is going to get retrived then

      use

      select single * from /BI0/MProfit_Ctr into wa_profit_Ctr

      where PROFIT_CTR = <some variable having ur profit_ctr>

      then update /BI0/MProfit_Ctr with wa_profit_Ctr.

      if i'm wrong then please explain ur requirement for us to help u better.

      regards,

      kartik

      Edited by: kartik tarla on Dec 20, 2008 9:23 AM

  • author's profile photo Former Member
    Former Member
    Posted on Dec 20, 2008 at 04:04 PM

    Kartik,

    I tried to use the update statement but I keep getting the following error.

    E: Unable to interpret "WITH". Possible causes of error : Incorrect spelling or comma error.

    Now back to your post.

    The requirement is: I have a profitcenter (0Profit_CTR) which has master data in it. It has an attribute of 0CALDAY. I want to check the load to see three things:

    1. If there is a new profit center coming from R/3 which will not have a date associated with it so I want to add that profit center and also put current system date for it.

    2. If there is a profit center but no date then I want to put current system date for that profit center

    3. If there is a profit center also with a date (I don't care what date as long as there is a date with it) then leave it alone.

    So I tried the following: BUT this code seems to put current system date for every record coming in which is no use to me.

    ***Put everything from profit center (/BI0/MProfit_Ctr) into an internal table called : lt_profit_Ctr

    Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.

    ****Compare the load with the internal table

    Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.

    ****If they are comparable but the date is NULL

    if sy-subrc eq 0 AND lt_profit_Ctr-CALDAY IS INITIAL.

    ****Put current system date in

    RESULT = SY-DATUM.

    EndIF.

    I feel I need to also add an ELSE statement in which I can tell to leave the records alone that are already in M Table with a date.

    Also, Yes, we can use SELECT SINGLE as this coding is being done in a transfer routine and only one record at a time will be selected.

    Thank You,

    Syed.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      well the statement is

      update <tablename> from <workarea>

      I previously mentioned WITH replace it with FROM in the update statement it will work for surely

      and if select single can be used then it is suggeste to use it only

       select single * from /BI0/MProfit_Ctr into wa_profit_Ctr
      where PROFIT_CTR = <ur load>
      "then
       update /BI0/MProfit_Ctr from wa_profit_Ctr

      .

      Edited by: kartik tarla on Dec 20, 2008 10:04 PM

      Edited by: kartik tarla on Dec 20, 2008 10:07 PM

  • author's profile photo Former Member
    Former Member
    Posted on Dec 21, 2008 at 03:31 PM

    Kartik,

    Here is what I tried and got the following message...

    Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.

    loop at lt_profit_ctr into wa_profit_Ctr.

    if sy-subrc eq 0 AND wa_profit_Ctr-CALDAY IS INITIAL.

    wa_profit_Ctr-CALDAY = SY-DATUM.

    update /BI0/MProfit_Ctr FROM wa_profit_Ctr-CALDAY.

    EndIF.

    endloop.

    Error Message:

    The databae view "/BI0/MPROFIT_CTR" is write-protected, so it cannot be changed.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      hi syed,

      if u r not able to update ur database table, check its settings to make it writeable because as i understand ur requirement it is that if calday is initial then u need to update ur table with current date for tht u need to have write access of ur table.

      кu03B1ятu03B9к.

  • author's profile photo Former Member
    Former Member
    Posted on Dec 18, 2008 at 01:49 PM

    Use an AND condition instead of Or that u have used.

    Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.

    Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.

    if sy-subrc eq 0 AND lt_profit_Ctr-CALDAY IS INITIAL.

    RESULT = SY-DATUM.

    ENDIF

    Edited by: kartik tarla on Dec 18, 2008 7:19 PM

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 18, 2008 at 01:54 PM

    Hi,

    you have given,

    Select * from /BI0/MProfit_Ctr into table lt_profit_Ctr.

    Read table lt_profit_Ctr with key PROFIT_CTR = /BI0/MPROFIT_CTR.

    if sy-subrc 0 or lt_profit_Ctr-CALDAY IS INITIAL.

    RESULT = SY-DATUM.

    ENDIF

    So if there is a entry then sy-subrc will be '0' and the next part will be executed irrespective of your

    lt_profit_Ctr-CALDAY IS INITIAL'.

    What you need is when sy-subrc = 0 then check for the condition 'lt_profit_Ctr-CALDAY IS INITIAL'.

    So you can write either

    if sy-subrc 0.
     if lt_profit_Ctr-CALDAY IS INITIAL.
       RESULT = SY-DATUM.
     endif.
    endif.

    or

    if sy-subrc 0 AND lt_profit_Ctr-CALDAY IS INITIAL. "<= Note the and
    RESULT = SY-DATUM.
    ENDIF

    Hope this helps you.

    Regards,

    Manoj Kumar P

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 02:39 PM

    Flavya, Are you saying to do soemthing like this?

    Read table lt_COSTCENTER with key COSTCENTER = /BI0/MCOSTCENTER.

    Select * from /BI0/MCOSTCENTER into table lt_COSTCENTER.

    if sy-subrc <> 0 AND lt_COSTCENTER-CALDAY IS INITIAL.

    RESULT = SY-DATUM.

    EndIF.

    I don't think it'll work... It'll read the internal table and M table first, then select the records and then check for condition???????

    Syed.

    Add a comment
    10|10000 characters needed characters exceeded

    • >

      > Flavya, Are you saying to do soemthing like this?

      >

      > Read table lt_COSTCENTER with key COSTCENTER = /BI0/MCOSTCENTER.

      >

      > Select * from /BI0/MCOSTCENTER into table lt_COSTCENTER.

      >

      > if sy-subrc <> 0 AND lt_COSTCENTER-CALDAY IS INITIAL.

      >

      > RESULT = SY-DATUM.

      > EndIF.

      >

      >

      > I don't think it'll work... It'll read the internal table and M table first, then select the records and then check for condition???????

      >

      >

      > Syed.

      You're right, it won't work. It made no sense, so I've removed his answer.

      Regards

      matt

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 09:20 PM

    I think what I need in my code is some sort of a breakout statement where If all conditions are met, I don't want to do anything. I am trying to find the syntax for it in ABAP.

    Syed.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.