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

Select record which is valid for current date

Former Member
0 Likes
2,514

Hi Experts,

I am writing the below logic to check if the record is valid or not:

SELECT * UP TO 1 ROWS FROM ZTAB

INTO L_ZHYPENT WHERE RCOMP = I_KNA1-VBUND
ORDER BY EFF_TO DESCENDING  .
ENDSELECT.
IF SY-SUBRC = 0.
IF SY-DATUM LT L_ZHYPENT-EFF_FROM OR SY-DATUM > L_ZHYPENT-EFF_TO.
MESSAGE TEXT-011 TYPE 'E'.
ENDIF.

But the above logic will pick the second entry which will be invalid. I want to select record which is valid for current date.

Any suggestions on this?

Database:

RCOMP    EFF_TO     EFF_FROM      

109035     2012012     2011006

109035     9999012     2013001

8 REPLIES 8
Read only

former_member189779
Active Contributor
0 Likes
1,656

if RCOMP is complete key of your table. Try Select Single * from

ZTAB

INTO L_ZHYPENT WHERE RCOMP = I_KNA1-VBUND

Read only

0 Likes
1,656

My table key are RCOMP and  EFF_TO

Read only

0 Likes
1,656

Try remove order by.

SELECT * UP TO 1 ROWS FROM ZTAB

INTO L_ZHYPENT WHERE RCOMP = I_KNA1-VBUND
ENDSELECT.

Read only

Former Member
0 Likes
1,656

You need to check that today's date is greater than the effective date and less than the end date.

Forget about up to n rows and order by.

Rob

Read only

0 Likes
1,656

Hi Rob,

So should i select all the data in my table and then check the dates?

Becuase if there is no data in the ztable for the condition i dont have to display the error message

Read only

0 Likes
1,656

Nonono

Add the date selection to the WHERE.

Rob

Read only

0 Likes
1,656

If i add date selection in where clause and there is no entry in the table then also there will be a error message which will be incorrect:

SELECT * UP TO 1 ROWS FROM ZTAB

INTO L_ZHYPENT WHERE RCOMP = I_KNA1-VBUND
                                         and EFF_TO GE sy-datum  .
ENDSELECT.
IF SY-SUBRC <> 0 . "even if there is no entry in ZTAB for the conditionit will display error message
MESSAGE TEXT-011 TYPE 'E'.
ENDIF.

Read only

0 Likes
1,656

This is how i made it work but i dnt like loop...endloop 😞

    SELECT *  FROM ZHYPENT
      INTO  TABLE LT_ZHYPENT
      WHERE RCOMP = I_LFA1-VBUND.
*--end of RD1K920476
     IF SY-SUBRC = 0.
       LOOP AT LT_ZHYPENT INTO LW_ZHYPENT WHERE EFF_FROM LE SY-DATUM
                                          AND   EFF_TO   GE SY-DATUM.
       ENDLOOP.
       IF SY-SUBRC <> 0.
       MESSAGE TEXT-011 TYPE 'E'.
      ENDIF.
     ENDIF.