cancel
Showing results for 
Search instead for 
Did you mean: 

SQL stmt with LIKE and variable!

Former Member
0 Kudos

I would like to use a SQL stmts like this:

SELECT * FROM tab WHERE key1 LIKE '%<i>variable</i>%'.

Using LIKE in the WHERE clause requieres a character field. But I want to use a vairable...

how should i write that?

thx,

holger

Accepted Solutions (0)

Answers (6)

Answers (6)

sreemsft
Contributor
0 Kudos

Hi Holger,

You can do like this also.

Declare a range table like this.

RANGES: R_VBELN FOR VBAK-VBELN. (You can declare your own variable type here.)

Then populate the variable into the Ranges like this.

R_VBELN-LOW = VARIABLE.

R_VBELN-SIGN = 'I'.

R_VBELN-OPTION = 'CP'.

APPEND R_VBELN.

CLEAR R_VBELN.

Then write a select like this.

SELECT * FROM TABLE WHERE VBELN IN R_VBELN.

The above select will achieve the same functionality.

Thanks,

Sreekanth

Former Member
0 Kudos

Hi Holger,

You can use 'LIKE' for Character string or variable in

Where clause of Select statement.

Pass the value which needs to be compared into a variable and use that variable in the select statement.

Example:

Data: v_ltext like cskt-ltext value 'abc%abc'.

Select * from CSKT

Into table itab

where ltext like v_ltext.

Regards,

Sudhakar.

Former Member
0 Kudos

hi,

data str(100).

data val type i.

now you can dynamically set the value for str..like-->

case flag.

when '1'.

val = value for flag 1.

str = concatenate 'key1 eq ' val into str.

when '2'.

val = value for flag 1.

str = concatenate 'key1 eq ' val into str.

endcase.

SELECT * FROM tab WHERE str.

i hope it will solve your problem.

Message was edited by: Anid

Former Member
0 Kudos

Hi Holger,

1. Just a slight change in sql

2. Instead of hardcode ther where condition with LIKE statement we have to CONSTRUCT the whole condition

in a variable and use.

3. WHERE (source_text)

source_text is a variable -- maybe (100) type c.

4. Just consturct the source_text

using CONCATENATE

so that it contains

key1 like '%ABC'.

5. It will work.

6. Make proper use of concatenate and variable

and proper Quotes '

otherwise sql at runtime will give error.

Regards,

Amit M.

Former Member
0 Kudos

hi,

Check out this sample code.

concatenate sy-cprog '%' into <b>w_bdcerr</b>.

SELECT * FROM APQI UP TO 1 ROWS

WHERE GROUPID LIKE <b>W_BDCERR</b> AND

QSTATE NE 'F'.

ENDSELECT.

You concatenate the variable and % into a string and use that in select. This meets your requirement.

Regards,

Sailaja.

abdul_hakim
Active Contributor
0 Kudos

Hi

This variant of SELECT Clause wont support varible handling here..

Check the below link as well..

http://help.sap.com/saphelp_46c/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

Regards,

Abdul

Message was edited by: Abdul Hakim