1

I have table column ZZPATTN which stores patterns like 01-001\*, 01-002\*, etc. and I have the variable IM_NAME which contains values like 01-001-ABC, 01-002-DEF, etc.

In SELECT statement WHERE clause, when I write @IM_NAME LIKE ZZPATTN, when click Check button will show this grammar error (error display cursor in front of @IM_NAME), even though I change \* to %:

"@IM_NAME LIKE ZZPATTN" is invalid here (due to grammar). contains an invalid character or it is a keyword. (It might be possible to escape it using "!"). Or a space is missing or there is one space too many.

I tried using other ways to match, but functions REPLACE(), LEFT(), SUBSTRING() are all unknown. Appreciate any advise thanks.

Code:

SELECT ZZPRICE, ZZEFDAT INTO ( @PRICE, @EFDAT ) FROM ZZPRICE
            UP TO 1 ROWS
            WHERE ( ( 'X' <> @LV_FLG AND ZZNAME = @IM_NAME ) OR
              ( 'X' = @LV_FLG AND @IM_NAME LIKE ZZPATTN ) ) 
ENDSELECT.

The ABAP version is 7.40.

1
  • IMO: UP TO 1 ROWS should be replaced by a SELECT SINGLE if you don't care which record/row is provided, or an ORDER BY should be added if you do. I don't like the way you manage the LV_FLAG, and it's not the natural hate I have for hungary notation, it's because you are making the whole thing difficult to read (then to maintain). If you don't want to put two SQLs in an IF-ELSE-ENDIF block, you can use a string condition and build it within that IF-ELSE-ENDIF block and then WHERE (cond). And I'd put the columns always on the left of the condition. Commented Feb 26 at 15:24

2 Answers 2

1

This syntax @variable LIKE column is not accepted in ABAP 7.58.

Something like @variable = REPLACE( column, '*', ' ' ) is accepted in ABAP 7.58.

The possible functions depend on the ABAP version you are using. I can't propose a solution as I don't know the target version.

You may use Native SQL to query directly the database, to avoid ABAP restrictions.

For your information, SQL functions may be indicated on the right only since ABAP 7.55, and variables historically used to be allowed only on the right side (I don't remember when it was allowed on the left).

Look at the ABAP documentation to see what is allowed. For instance, here is the latest ABAP documentation: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap.htm. You may look at the release news or you may access the ABAP documentation of other versions elsewhere (from your ABAP environment for instance).

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the info! I checked the latest documentation, it really helps. fyi the version i am using is 7.4, I guess some functions are not provided yet.
0

Since I can't use @IM_NAME LIKE ZZPATTN in the SELECT statement, I use another alternative: TYPES, RANGE OF and IN. I split the IM_NAME and append them to an internal table, and use IN to get the results. The reason I used TYPES, RANGE OF not TYPE, TABLE OF is to solve error "The line structure of the table "LT_PATTERNS" is incorrect."

Not straightforward as LIKE, but could provide similar result. Here are the sample code:

TYPES:
  ty_pattern TYPE STRING,  " Define type for pattern
  ty_pattern_range TYPE RANGE OF ty_pattern.  " Range table for pattern
DATA:
  lt_patterns TYPE ty_pattern_range,    " Range table for patterns
  lv_pattern TYPE STRING,       " Current pattern
  lv_length TYPE i.

" Initialize the pattern with '*' (wildcard)
lv_pattern = '*'.
CLEAR lt_patterns.
APPEND VALUE #( sign = 'I' option = 'EQ' low = lv_pattern ) TO lt_patterns.  " Add pattern to range

lv_length = strlen( IM_NAME ).  
  WHILE lv_length >= 1.
    lv_pattern = substring( val = IM_NAME len = lv_length ) && '*'.  
    lv_length = lv_length - 1.  " Reduce the length
    APPEND VALUE #( sign = 'I' option = 'EQ' low = lv_pattern ) TO lt_patterns.  
  ENDWHILE.

" Now use the range table in the SELECT statement
  SELECT ZZPRICE, ZZEFDAT INTO ( @PRICE, @EFDAT ) FROM ZZPRICE
        UP TO 1 ROWS
        WHERE ( ZZNAME = @IM_NAME OR ZZPATTN IN @lt_patterns ) 
ENDSELECT.

1 Comment

Not sure because I use to use ranges and not LIKE direct sentences, but I guess one of your troubles comes because LIKE in ABAP SQL uses % and not * . Am I right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.