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

dynamic query not working

Former Member
0 Likes
1,150

hi

im using a dynamic query to put in an internal table but its not working

Can you see whats wrong in that?

gv_query = ' au~uname AS OTCTUSERNM,'.

CONCATENATE gv_query 'a1object' '.' 'a1field' ' AS 0TCTAUTH' INTO gv_query.

CONCATENATE gv_query 'au~to_dat AS OTCTADTO, OTCTOBJNM, ' INTO gv_query.

SELECT DISTINCT (gv_query)

INTO CORRESPONDING FIELDS OF TABLE gi_ds1

FROM agr_users AS au INNER JOIN

agr_1251 AS a1

ON auagr_name = a1agr_name.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,100

Hello,

Following are the observations:

1. gv_query = ' au~uname AS OTCTUSERNM,'.

>> In above string i think you want to use uname column as OTCTUSERNM and it is correct

2. CONCATENATE gv_query 'a1object' '.' 'a1field' ' AS 0TCTAUTH' INTO

gv_query.

>> In statement 2 above I am not able to understand whey 'a1object' '.' 'a1field' this is incorrect.

Also '.' (dot) in not required in query it will be either '~' or 'AS' as valid chars.

3. CONCATENATE gv_query 'au~to_dat AS OTCTADTO, OTCTOBJNM, ' INTO gv_query.

>> In statement 3 above whey OTCTOBJNM is used there is no such column exit in AGR_USERS.

4. SELECT DISTINCT (gv_query)

INTO CORRESPONDING FIELDS OF TABLE gi_ds1

FROM agr_users AS au INNER JOIN

agr_1251 AS a1

ON auagr_name = a1agr_name.

>> In above statement a1 is used as alias for AGR_USERS then why it it not there in statement 3 where instead au is used.

Normarlly query is like this

'<alias1><col_name1> as <col_desc1>, <alias2><col_name2> as <col_desc2>......up to n cols

In this case it is possible that alias1 and alias2 are used for different table in case of joins and for same table in case of self joins.

Finally, You can only define colums in dynamic query those are available in the table from which we have to select the data.

Hope this helps.

Thanks,

Augustin.

10 REPLIES 10
Read only

Former Member
0 Likes
1,100

'.'

What's it doing there?

You better print out the value in gv_query to see how it actually looks like. That might give you some hints as well.

Read only

0 Likes
1,100

the '.' is the concatenation of 2 fields. Is it the correct way of merging 2 fields in a dynamic query?

Read only

0 Likes
1,100

Actually, i don;t know: haven't used it like that.

But, my advice stands: print out the content of gv_query and you'll find a few more errors.

Read only

Former Member
0 Likes
1,100

If I am not wrong then '.' is not required I hope so.

You can try like this

gv_query = 'au~uname AS OTCTUSERNM'.
CONCATENATE gv_query 'a1~object a1~field AS 0TCTAUTH' INTO gv_query SEPARATED BY space.

CONCATENATE gv_query 'au~to_dat AS OTCTADTO' INTO gv_query SEPARATED BY space.

It may work

Regards

Shiba Prasad Dutta

Read only

Former Member
0 Likes
1,101

Hello,

Following are the observations:

1. gv_query = ' au~uname AS OTCTUSERNM,'.

>> In above string i think you want to use uname column as OTCTUSERNM and it is correct

2. CONCATENATE gv_query 'a1object' '.' 'a1field' ' AS 0TCTAUTH' INTO

gv_query.

>> In statement 2 above I am not able to understand whey 'a1object' '.' 'a1field' this is incorrect.

Also '.' (dot) in not required in query it will be either '~' or 'AS' as valid chars.

3. CONCATENATE gv_query 'au~to_dat AS OTCTADTO, OTCTOBJNM, ' INTO gv_query.

>> In statement 3 above whey OTCTOBJNM is used there is no such column exit in AGR_USERS.

4. SELECT DISTINCT (gv_query)

INTO CORRESPONDING FIELDS OF TABLE gi_ds1

FROM agr_users AS au INNER JOIN

agr_1251 AS a1

ON auagr_name = a1agr_name.

>> In above statement a1 is used as alias for AGR_USERS then why it it not there in statement 3 where instead au is used.

Normarlly query is like this

'<alias1><col_name1> as <col_desc1>, <alias2><col_name2> as <col_desc2>......up to n cols

In this case it is possible that alias1 and alias2 are used for different table in case of joins and for same table in case of self joins.

Finally, You can only define colums in dynamic query those are available in the table from which we have to select the data.

Hope this helps.

Thanks,

Augustin.

Read only

0 Likes
1,100

Hi Augustarian

I need the concatenation of the two fields as OCTCTAUTH i.e a1object.a1field

Read only

0 Likes
1,100

Hello Anjali,

There are two things:

1. you want to contcatenate two columns and give them in query for fetching result which is impossible as

ABAP sql require well formed SQL and concatenation is not allowed in query.

2. you want to concatenate output of two columsn so in that case first get values of both the fields in internal table and then

either in SELECT..ENDSELECt or in internal table LOOP you can go on concatenating values for required column.

Finally, concatenating using "." is not allowed in ABAP query.

It has nothing to do with dynamic query. if you try same thing using static query that will give you same error.

Hope this helps,

Augustin.

Read only

0 Likes
1,100

oh i see

i didnt know concatenation is not allowed during dynamic query

in fact its because of the concatenation that i did a dynamic query

is there other alternative or i should do a loop to concatenate the fields?

plz advise

Read only

0 Likes
1,100

Hello Anjali,

Best practice in ABAP is to query the data into internal table and then process it in internal table loop.

So you can do concatenation in loop it will be much faster than SELECT..ENDSELECT loop.

Thanks,

Augustin.

Read only

0 Likes
1,100

thanks a lot Augustin