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 single

Former Member
0 Likes
842

Hi Guys,

I have a strange requirement as stated below :

Kindly replace the following select statement by select single. I doubt if it is possible.

SELECT matnr

dgcl

INTO TABLE pt_dgtmd[]

FROM dgtmd

FOR ALL ENTRIES IN pt_matd[]

WHERE matnr = pt_matd-matnr.

IF sy-subrc = 0. ENDIF.

Appreciate with max points if any body could help at the earliest.

Regards,

Shankar

9 REPLIES 9
Read only

Former Member
0 Likes
812

You have internal table

loop at pt_matd.

SELECT single matnr dgcl

INTO pt_dgtmd -> this is work area

FROM dgtmd

WHERE matnr = pt_matd-matnr.

if sy-subrc eq 0.

endif.

endloop.

Thanks

Seshu

Read only

alejandro_bindi
Active Contributor
0 Likes
812

You're selecting a group of records (into table pt_dgtmd) which join with another group of records (in pt_matd).

If you intend to select a single record, your source should also be a single record. If you must select the first one, you could do this:


CLEAR wa_dgtmd.
LOOP AT pt_matd.
  SELECT SINGLE matnr dgcl
  INTO wa_dgtmd 
  FROM dgtmd
  WHERE matnr = pt_matd-matnr.

  IF sy-subrc = 0.
    EXIT.
  ENDIF.
ENDLOOP.

IF NOT wa_dgtmd IS INITIAL.
  " You've found your single record
ELSE.
  " You didn't
ENDIF.

Regards.

One more thing, is matnr the primary key of dgtmd table? Because if not, you shouldn't be trying to use SELECT SINGLE, you could try SELECT...UP TO 1 ROWS instead.

Message was edited by:

Alejandro Bindi

Read only

Former Member
0 Likes
812

Guys,

Thanks for your immediate replies. Since the table PT_MATD does not have an header line it is not possible to loop this table. Moreover this table is declared as types.

Please help.

Regards,

Shankar

Read only

0 Likes
812

Add commented code:


DATA: wa_matd LIKE LINE OF pt_matd.  "<<< Added code

CLEAR wa_dgtmd.
LOOP AT pt_matd INTO wa_matd.  "<<< Modified code
  SELECT SINGLE matnr dgcl
  INTO wa_dgtmd 
  FROM dgtmd
  WHERE matnr = wa_matd-matnr.  "<<< Modified code
 
  IF sy-subrc = 0.
    EXIT.
  ENDIF.
ENDLOOP.
 
IF NOT wa_dgtmd IS INITIAL.
  " You've found your single record
ELSE.
  " You didn't
ENDIF.

Regards

Read only

0 Likes
812

you have internal table without header

then declare work area

data wt_matd like line of pt_matd.

data wt_dgtmd like line of pt_dgtmd.

loop at pt_matd into wa_matd.

SELECT single matnr dgcl

INTO TABLE wt_dgtmd

FROM dgtmd

WHERE matnr = wt_matd-matnr.

IF sy-subrc = 0.

ENDIF.

endloop.

Thanks

Seshu

Read only

Former Member
0 Likes
812

Thanks buddy, I am not getting any syntax error.

Once the user comes back, let me give u more points.

Regards,

Shankar

Read only

Former Member
0 Likes
812

I hope that you are aware that the original select would be the better choice than the loop select single.

Read only

0 Likes
812

Hi Boes,

The original select statement gives me the ABAP Run time dump.

Regards,

Shankar

Read only

Former Member
0 Likes
812

Hi

Select Single is mainly used for validation perpouse , even it will get all the data from the database table

SELECT <b>single</b> matnr dgcl

INTO TABLE pt_dgtmd[]

FROM dgtmd

FOR ALL ENTRIES IN pt_matd

WHERE matnr = pt_matd-matnr.

IF sy-subrc = 0. ENDIF.

reward if usefull