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

Convert data field to string

Missschaaa
Participant
0 Likes
3,826

Hello guys,

is it possible to convert any type of data field to a string? My problem is the following: I use dynamic sub routine where I for example do some data select but also need the name of the database table as a string. So in first case I would use ZTABLE as data field which I can use as SELECT table and in the second case I need 'ZTABLE' as string field (only the name, not the content) which I need to move to some other variable. Is there any function or hint how I can achieve this? Thought about field symbols but I'm not sure if its possible.

Regards Michael

10 REPLIES 10
Read only

FredericGirod
Active Contributor
3,763

You mean :

data(table_name) = conv tablename( 'T001W' ).

select * from (table_name) ....

assign (table_name) to field-symbol(<my_table>).

and why you need to do this ?

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,763

It's probably possible, but I don't get what is your exact question, so difficult to answer.

Read only

FredericGirod
Active Contributor
0 Likes
3,763

maybe is to catch the result of the SELECT, but it is more simple to create dynamic internal table

Read only

Missschaaa
Participant
0 Likes
3,763

No vice versa. I use the variable itself in this case T001W as data object and need in some variable myvariable which should have 'T001W' as value. I do not want to just say myvariable = 'T001W' because it should be dynamic for more than one variable. I coded a macro which should dynamic read data from different custom tables and I just want to get coding as low as possible. In the macro I need both, the table as table name and the table as string in some data field.

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,763

Why don't you show your code?

If it's a macro, there's no issue to pass only T001W, which your macro can use as both &1 and '&1'.

But you may also use the macro with 'T001W' and do SELECT ... FROM (&1).

I wouldn't use a macro anyway, only experts can debug, nowadays you can avoid the macros completely.

Read only

Missschaaa
Participant
0 Likes
3,763
  DEFINE mac_get_conttxt.
CLEAR: ls_condtab,
lt_condtab.

ls_condtab-field = 'SPRAS'.
ls_condtab-opera = 'EQ'.
ls_condtab-low = sy-langu.
APPEND ls_condtab TO lt_condtab.
ls_condtab-field = &4.
ls_condtab-opera = 'EQ'.
ls_condtab-low = is_not_detail-&5.
APPEND ls_condtab TO lt_condtab.
CALL FUNCTION 'RH_DYNAMIC_WHERE_BUILD'
EXPORTING
dbtable = space
TABLES
condtab = lt_condtab
where_clause = lt_where_clause
EXCEPTIONS
empty_condtab = 01
no_db_field = 02
unknown_db = 03
wrong_condition = 04.

CLEAR ls_desc.
SELECT SINGLE rollname FROM dd03l INTO ls_desc-rollname
WHERE tabname EQ &3
AND fieldname EQ &4.

SELECT SINGLE &1 &2 FROM (&3) INTO (ls_desc-id, ls_desc-text)
WHERE (lt_where_clause).

IF ls_desc-id IS NOT INITIAL
AND ls_desc-text IS NOT INITIAL.
APPEND ls_desc TO lt_desc.
ENDIF.
END-OF-DEFINITION.

macro looks like this. &1 and &2 is for example fieldname as fieldname itself, &4 is fieldname as string. I do not want to put it both in the macro (fieldname and 'FIELDNAME'), just one time and convert it for the the other statement.

Read only

FredericGirod
Active Contributor
0 Likes
3,763

I read several time your question, and I still doesn't understand, you already did it:

 SELECT SINGLE &1 &2 FROM (&3) INTO (ls_desc-id, ls_desc-text)<br>      WHERE (lt_where_clause).
    SELECT SINGLE rollname FROM dd03l INTO ls_desc-rollname<br>      WHERE tabname   EQ &3<br>        AND fieldname EQ &4.

and, you should really forget MACRO, this is old old school

Read only

Missschaaa
Participant
0 Likes
3,763

Yes, your hint for database table was good, I already fixed it, but I still have the problem for fieldnames of database tables (&4 vs &2).

I know macro is not very popular here, but I like it. Yes, debugging is crap, but its an easy way to put some repeating routines more cleaned up in coding. Of course I could also use some form routines or methods, but in my mind its not that flexible as macro, I canot put simple fieldnames withoud structure name in it like in macro for example.

Read only

FredericGirod
Active Contributor
0 Likes
3,763

maybe have a look to the demo program available through ABAPDOCU trans : demo_dynamic_sql

(and also through SE38)

You could do whatever you made in macro, in method. the demo program show using method

Read only

Sandra_Rossi
Active Contributor
3,763

We could add a second reason why macros should be avoided because it may be very difficult to troubleshoot syntax errors... 😄

Conclusion: don't use macros because it's very difficult to debug and to troubleshoot syntax errors.