Download SAP Query Output to Local/ Network Folders in Background Applies to: SAP release where SQUE0001 enhancement (SMOD) available For more information, visit the ABAP homepage. Summary This article serves as a guide to download SAP Query output to network folder when executing the query in background. Author: Aditya Laud Company: nvidia Graphics Pvt. Ltd. Created on: 17 December 2010 Author Bio Aditya Laud is a SAP ABAP developer working with nvidia Graphics Pvt. Ltd in India. He is a member of the in house SAP Application, Development, Maintenance and Support team. 2010 SAP AG 1
Table of Contents Requirement... 3 Challenge with File Store... 3 Solution/ Approach... 3 Enhancement (SMOD) SQUE0001... 3 Enhancement (SMOD) Implementation... 3 Define download internal table... 4 Add header to download table... 4 Build body of download table... 4 Logic to determine download file location/ path... 5 Impact of Enhancement... 5 Related Content... 6 Copyright... 7 2010 SAP AG 2
Requirement Download SAP query output in XLS format to network folder when executing query in background. Challenge with File Store The File Store option for download has a pop up which requires user action, hence this option cannot be used in background run mode. Solution/ Approach Enhancement (SMOD) SQUE0001 This enhancement gives access to the SAP query result table DATATAB and the LISTDESC table which contains the headings for each column in DATATAB table. When this enhancement is activated (via project in CMOD) an additional radio button Private File is added to the Output Format section in query selection screen. When user selects this option and executes the query then this enhancement (user exit) is triggered from which the query result can be downloaded. Note: Private File option is displayed when SQUE0001 enhancement is activated & query is regenerated Enhancement (SMOD) Implementation To implement the enhancement create a project through CMOD transaction and create user exit include ZXQUEU01. This include has access to two tables viz. DATATAB and LISTDESC. DATATAB - internal table contains the SAP query result LISTDESC internal table contains the SAP query output header(s) o o FDESC contains the header text e.g. Material Number FPOS contains the position of the column in the output Since the user exit implementation needs to be generic DATATAB and LISTDESC would contain different columns and number of records (respectively) at run time hence the download table needs to be built at runtime. Following is an example for building a tab delimited internal table for downloaded to network folder: 2010 SAP AG 3
Define download internal table Create an internal table with single component TYPE STRING. The header and item columns would be concatenated with tab separator (CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB) into the download table component. Add header to download table Create the header line in download internal table from LISTDESC table by concatenating the FDESC field in LISTDESC into the download table string component. Example of LISTDESC table: Note: Only select those records from LISTDESC where LID (list ID) is equal to LIST_ID field which is an importing parameter to the user exit. Build body of download table To add the body (query result from DATATAB) to the download internal table LT_DOWNLOAD in tab delimited format use the following logic: For each record in DATATAB table loop through LISTDESC where LID = LIST_ID Assign component LISTDESC-FPOS of DATATAB structure to a field symbol this stores the FPOS component in the field symbol While looping through LISTDESC concatenate all components into a string variable separated by tab delimiter (CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB). At end of looping at LISTDESC for each record of DATATAB append the string to a download internal table (into which the header has already been added as explained in section Add header to download table) At end of looping at DATATAB the table is ready for downloading to data set Please take care to ensure the format of the DATATAB components is correct e.g. when dealing with DATE and CURR fields. Note: Since the DATATAB table is dynamic (depends on query being run) the above code is required to build final internal table for download. 2010 SAP AG 4
Download table to network folder Use the OPEN/ TRANSFER/ CLOSE dataset commands to download the internal table resulting from section Build body of download table to requested download file path. Note: Refer section Logic to determine download file location to determine requested download file path Logic to determine download file location/ path Below logic is designed to read the requested download file location from the File Store selection screen parameter in Output Format section of query. This approach avoids needing to maintain a Z table for reading the file s location from. The File Store option is as shown below where \\network\file.xls is entered. The selection screen parameter is %PATH. When the enhancement is triggered, the SAP query program is the calling program in the call stack, hence via field symbol, the %PATH variable can be read and used as the target file (for download). Code snippet for the same: Note: PROGRAM is an importing parameter for the user exit function which contains the SAP query program name Impact of Enhancement Since the enhancement is triggered ONLY when user selects the Private File radio button on the selection screen of SAP query there is no impact to existing reports or their reporting. To display the Private File mode (after implementing and activating the enhancement) the SAP query has to be regenerated. Note: To re-generate the SAP query go to transaction SQ00, select query in change mode and click SAVE 2010 SAP AG 5
Related Content http://help.sap.com/saphelp_40b/helpdata/fr/d2/cb477d455611d189710000e8322d00/content.htm https://cw.sdn.sap.com/cw/docs/doc-41323 For more information, visit the ABAP homepage. 2010 SAP AG 6
Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document. 2010 SAP AG 7