12-18-2008 1:44 PM
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.
12-18-2008 1: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!
12-18-2008 1: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
12-18-2008 1: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!
12-18-2008 1: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
12-19-2008 1: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.
12-19-2008 1:57 PM
<<Utter nonsense answer removed by moderator. No idea why he was given points for it...>>
Edited by: Matt on Dec 19, 2008 9:40 PM
12-19-2008 2:00 PM
hi,
I have gone thru your problem, i understood that u r sending the current date to field RESULT..
But i could not get after that what r u doing, i mean to say if u r updating database table with value RESULT then u can find the value in database..either u execute program today or tomorrow.....
As far as my knowledge u r not updating it to database..i think u r generating a list...
thats why when ever u execute that u will get that day date as output...
For ur requirement, i feel update the table with value result...
Rgds.,
subash
12-19-2008 4:17 PM
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.
wa-CALDAY = SY-DATUM. "Use work area of type lt_profit_ctr
update /BI0/MProfit_Ctr with wa transporting calday.
EndIF.
one more thing are u sure u need to use read table, i think u need to use loop at so that all records with the specified condition get updated.
regards,
Kartik
12-19-2008 9:04 PM
>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.
12-19-2008 2: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.
12-19-2008 8:42 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.
You're right, it won't work. It made no sense, so I've removed his answer.
Regards
matt
12-19-2008 4:28 PM
Kartik,
Can you give me the data declaration for this??? not too familiar with work areas.
ThANKS,
Syed.
12-19-2008 4:35 PM
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.
12-19-2008 8: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
12-20-2008 3:52 AM
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
12-19-2008 9: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.
12-20-2008 4: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.
12-20-2008 4:22 PM
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
12-20-2008 11:59 PM
12-21-2008 3: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.
12-21-2008 3:56 PM
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к.
12-21-2008 3:46 PM
Here is what I tried after last try:
TABLES: /bi0/Mprofit_Ctr.
DATA: lt_profit_Ctr like /bi0/Mprofit_Ctr occurs 0 with header Line.
DATA: yest_day type d.
CODE:
yest_day = sy-datum - 1.
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 NOT INITIAL.
RESULT = yest_day.
ELSE.
RESULT = SY-DATUM.
ENDIF.
All of the records in M Table had date (CALDAY) 12/21/2008 and same records came through the load as well. Which statement should've been executed?
Thanks,
Syed.
12-21-2008 3:58 PM
what is the variable RESULT that u r using? whts its significance?
кu03B1ятu03B9к.
12-21-2008 4:00 PM
Kartik,
That's weird because I've been able to change dates without any problems just this updating won't do that. Is there any other way to do this without updating?
I am sorry I didn't understand the following:
"what is the variable RESULT that u r using? whts its significance?"
Thanks,
Syed.
12-21-2008 4:18 PM
TABLES: /bi0/Mprofit_Ctr.
DATA: lt_profit_Ctr like /bi0/Mprofit_Ctr occurs 0 with header Line.
DATA: yest_day type d.
CODE:
yest_day = sy-datum - 1.
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 NOT INITIAL.
RESULT = yest_day.
ELSE.
RESULT = SY-DATUM.
ENDIF.
In the above code i wanted to know the significance of result. what it is being used for?
And if update is not working try
modify
modify /BI0/MProfit_Ctr FROM wa_profit_Ctr-CALDAY.
12-21-2008 4:24 PM
Result is a SAP given variable in transfer routine. When we write codes in BW we must have RESULTS (I know in transfer routine atleast which is what I am working at). After the execution of code RESULT will have the final value. I will try to use modify and see what happens.
Thanks,
Syed.
12-21-2008 4:27 PM
12-21-2008 4:55 PM
TABLES: /bi0/Mprofit_Ctr.
DATA: lt_profit_Ctr like /bi0/Mprofit_Ctr occurs 0 with header Line.
DATA: yest_day type d.
CODE:
yest_day = sy-datum - 1.
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 .
if lt_profit_Ctr-CALDAY IS NOT INITIAL.
RESULT = lt_profit_ctr-calday.
ELSE.
RESULT = SY-DATUM.
ENDIF.
endif.
try this if it doesn't work then probably u need some bapi to update ur table.
кu03B1ятu03B9к.
Edited by: kartik tarla on Dec 21, 2008 10:26 PM
12-21-2008 9:27 PM
Kartik,
Although your code looks good to me but it still didn't work. It changed all dates to SY-DATUM.
Syed.
12-21-2008 10:32 PM
Although one sdner tried to help a lot but couldn't resolve the issue. Will try to post this other forums in hope to get a resolution.
12-21-2008 10:51 PM
Please don't cross-post to other SDN forums, if that is what you meant.
Participation here is voluntary... you need to be a bit patient in the voluntary world... and to attract the attention of the gurus it needs to be interesting beyond that which is searchable.
Just my 2 cents,
Julius