‎2022 May 06 9:44 AM
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
‎2022 May 06 10:08 AM
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 ?
‎2022 May 06 10:21 AM
It's probably possible, but I don't get what is your exact question, so difficult to answer.
‎2022 May 06 10:25 AM
maybe is to catch the result of the SELECT, but it is more simple to create dynamic internal table
‎2022 May 06 10:36 AM
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.
‎2022 May 06 10:45 AM
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.
‎2022 May 06 10:46 AM
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.
‎2022 May 06 12:39 PM
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
‎2022 May 06 1:00 PM
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.
‎2022 May 06 1:09 PM
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
‎2022 May 06 3:44 PM
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.