Application Development 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: 

Contents within a variable moved to a literal

Former Member
0 Kudos

I have not used field-symbols yet. Is that the way to do the following?

I need to append to a table whose name I want to derive based on the fiscal year (bdatj). Each record in the internal table will have a bdatj. So if bdatj contains 2006, I want to append to table 2006_tab. If bdatj is 2007, I want to append to table 2007_tab.

Is there a way to do this using field-symbols instead of if statements based on the value in the field. This would be something like: If bdatj = '2006', then append record to 2006_tab, etc.

Thank you for any help.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Rich,

Thank you. This looks great. Just one more question. I really am in the dark as far as the years go (how many and which years). Ultimately, I need to write out the data into files with the fiscal period and fiscal year in the name. Other than declaring the names of the downloaded files also (as you did the internal tables) is there any way to give a name to the file based on the year in the record in the itab dynamically? I seem to remember syntax something like &variable that gave you the contents of the variable that you could then concatenate into a string. It would look like

concatenate 'File' &bdatj '.txt' into filename. If bdatj was 2006 the filename would be File2006.txt.

8 REPLIES 8

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Sure, please see the following sample program.



report zrich_0001 .

data: begin of itab occurs 0,
      bdatj type sy-datum,
      end of itab.


data: begin of 2006_tab  occurs 0,
      bdatj type sy-datum,
      end of 2006_tab.


data: begin of 2007_tab  occurs 0,
      bdatj type sy-datum,
      end of 2007_tab.

data: table_name(30) type c.
field-symbols: <fs_tab> type table,
               <fs_wa>,
               <fs>.

* Generate some data in ITAB
itab-bdatj = '20061225'.  append itab.
itab-bdatj = '20061226'.  append itab.
itab-bdatj = '20061227'.  append itab.
itab-bdatj = '20061228'.  append itab.
itab-bdatj = '20061229'.  append itab.
itab-bdatj = '20061230'.  append itab.
itab-bdatj = '20061231'.  append itab.
itab-bdatj = '20070101'.  append itab.
itab-bdatj = '20070102'.  append itab.
itab-bdatj = '20070103'.  append itab.
itab-bdatj = '20070104'.  append itab.
itab-bdatj = '20070105'.  append itab.
itab-bdatj = '20070106'.  append itab.

* Fill up the 2006 and 2007 tables
loop at itab.

  concatenate itab-bdatj+0(4) '_tab[]' into table_name.
  assign (table_name) to <fs_tab>.
  concatenate itab-bdatj+0(4) '_tab' into table_name.
  assign (table_name) to <fs_wa>.
  assign component 'BDATJ' of structure <fs_wa> to <fs>.
  <fs> = itab-bdatj.
  append <fs_wa> to <fs_tab>.


endloop.



* Write out the tables
loop at 2006_tab.
  write:/ 2006_tab-bdatj.
endloop.


skip 5.

loop at 2007_tab.
  write:/ 2007_tab-bdatj.
endloop.

Former Member
0 Kudos

Rich,

Thank you. This looks great. Just one more question. I really am in the dark as far as the years go (how many and which years). Ultimately, I need to write out the data into files with the fiscal period and fiscal year in the name. Other than declaring the names of the downloaded files also (as you did the internal tables) is there any way to give a name to the file based on the year in the record in the itab dynamically? I seem to remember syntax something like &variable that gave you the contents of the variable that you could then concatenate into a string. It would look like

concatenate 'File' &bdatj '.txt' into filename. If bdatj was 2006 the filename would be File2006.txt.

0 Kudos

There are a number of ways to handle that, but to answer your question, yes the concatenate statement would work for naming your files.

concatenate 'File' itab-bdatj+0(4) '.txt' into filename.

Do you want to build the "year" tables dynamically?

Please remember to award points for helpful answer and mark you post as solved when solved completely.

Regards,

Rich Heilman

Former Member
0 Kudos

Yes. I would want to dynamically create the file names. I would have to use it in fm ws_download. Have you done that? I have only seen a literal filename used.

I have awarded points.

Thank you.

0 Kudos

Yes, I have built filenames on the fly many times, it works quite well, just use the concatenate statement above, I was think that you want to create the internal tables dynamically at runtime. If so, then there is a lot more coding that we have to do.

Regards,

Rich Heilman

0 Kudos

So I can use a variable for the filename in fm ws_download?

0 Kudos

Absolutely yes.... just declare the variable as the same type as the one used the WS_DOWNLOAD func mod.

In this case -

data: YourFileName LIKE RLGRAP-FILENAME.

Note that WS_DOWNLOAD is an obsolete FM.

0 Kudos

Janet,

Note also that WS_DOWNLOAD is not Unicode compliant. If your organization is planning to turn on Unicode, you will need to re-visit your program in the future.

I have a example here of how to implement the ABAP Objects version of WS_DOWNLOAD.

REPORT Z_EXAMPLE_DOWNLOAD.

tables : tvkbt. "Organizational Unit: Sales Offices: Texts

*-- Paramerter Declation

PARAMETERS FILENAME(128) DEFAULT 'C:\temp\zarea.txt' LOWER CASE.

*--- tvkbt internal table

DATA: BEGIN OF itab_tvkbt OCCURS 0,

vkbur TYPE tvkbt-vkbur,

bezei TYPE tvkbt-bezei,

END OF itab_tvkbt.

*--- download internal table

DATA: BEGIN OF itab_down OCCURS 0,

vkbur(4) TYPE c,

bezei(20) TYPE c,

END OF itab_down.

START-OF-SELECTION.

select vkbur bezei APPENDING CORRESPONDING FIELDS OF TABLE itab_down

from tvkbt

where spras = 'E'.

  • I have commented out the WS_DOWNLOAD because it is not Unicode compliant - Jake.

  • CALL FUNCTION 'WS_DOWNLOAD'

  • EXPORTING

  • codepage = ' '

  • filename = filename

  • filetype = 'DAT'

  • TABLES

  • data_tab = itab_down.

        • Now here is the OOP version - below.

  • I am making object-oriented versions of the original developer's fields.

  • This must be done to implement class objects, which are Unicode

  • compliant to achieve the download of the file.

*

  • Note that I have made a local data def called itab_down_rec. And

  • then I create an internal table of that data def called

  • itab_down_no_header. This is required in OOP as internal tables

  • in OOP do not permit header lines in tables.

data: file_name_as_string type string.

DATA: BEGIN OF itab_down_rec,

vkbur(4) TYPE c,

bezei(20) TYPE c,

END OF itab_down_rec.

DATA: itab_down_no_header LIKE TABLE OF itab_down_rec.

  • Move filename from selection screen to a data def of TYPE STRING.

move filename to file_name_as_string.

  • Copy the itab_down table to the OO version without a header line.

itab_down_no_header[] = itab_down[].

  • Call the class object's method to perform the download to client.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD

EXPORTING

FILENAME = file_name_as_string

WRITE_FIELD_SEPARATOR = 'X'

CHANGING

DATA_TAB = itab_down_no_header

EXCEPTIONS

FILE_WRITE_ERROR = 1

NO_BATCH = 2

GUI_REFUSE_FILETRANSFER = 3

INVALID_TYPE = 4

NO_AUTHORITY = 5

UNKNOWN_ERROR = 6

HEADER_NOT_ALLOWED = 7

SEPARATOR_NOT_ALLOWED = 8

FILESIZE_NOT_ALLOWED = 9

HEADER_TOO_LONG = 10

DP_ERROR_CREATE = 11

DP_ERROR_SEND = 12

DP_ERROR_WRITE = 13

UNKNOWN_DP_ERROR = 14

ACCESS_DENIED = 15

DP_OUT_OF_MEMORY = 16

DISK_FULL = 17

DP_TIMEOUT = 18

FILE_NOT_FOUND = 19

DATAPROVIDER_EXCEPTION = 20

CONTROL_FLUSH_ERROR = 21

NOT_SUPPORTED_BY_GUI = 22

ERROR_NO_GUI = 23

others = 24

.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

  • Flush the method call to the client's frontend.

CALL METHOD CL_GUI_CFW=>FLUSH

EXCEPTIONS

CNTL_SYSTEM_ERROR = 1

CNTL_ERROR = 2

OTHERS = 3.

IF SY-SUBRC <> 0.

endif.

        • Now here is the OOP version - above.