Logo DWBI.org Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
SAP Data Services

Working with Data Services Flatfiles

Updated on Oct 01, 2020

In this article we will try to cover all the possible scenarios related to flatfiles in SAP Data Services.

Requirement 1: Let us look into the first requirement: Generate respective flatfiles containing employee information for each department. So for each and every department we will generate flatfiles in a dynamic manner from our employee database table.

  • First of all we will create a Job in Data Services. The main objective is to generate flat files dynamically using WHILE LOOP transform in Data Services. This is how the final implementation Job will look like:
Job to generate dynamic flat files
Job to generate dynamic flat files
  • Now we will design a Dataflow to populate a temporary database template table to populate the distinct set of available Departments present in the EMP table. This table will contain the list of values of the departments together with a sequence number. We will use this sequence number to loop through the available LOV's using the While Loop transform.
Dataflow to populate LOV
Dataflow to populate LOV
  • Select the EMP database table as Source. Next place a Query transform to select the DISTINCT department numbers present in the EMP table.
QUERY DISTINCT Department
QUERY DISTINCT Department
  • Next we will generate sequence number for each of the distinct departments using the in-built Data Services function gen_row_num()
QUERY Generate Row Number
QUERY Generate Row Number
  • Lastly we place a template table as the target table to load the distinct set of departments in the employee table.
Target Template table
Target Template table
  • Next we define the following Variables and initialize the variable values to be used in WHILE loop.
Data Services Variables
Data Services Variables
  • Next we set the variable $INIT_CNT to 0. This variable will be used as a counter variable of the While Loop. The variable $LOOP_CNT is set to the maximum values of the Departments in the Employee table. This will be the number of times the while loop will execute which in turn will generate the respective employee flat files for each departments.
Variable Initialization Script
Variable Initialization Script
  • Now we take a look inside the WHILE Loop and see the final implementation logic to generate dynamic flat files for each department. The logic of the while loop is simple, that is it will iterate as many times as the distinct set of departments.
WHILE Loop
WHILE Loop
  • The below script sets the Department Number and the corresponding file that needs to be generated dynamically by the next following Dataflow. Here the variable initialization counter is incremented by 1. Next the variable $DEPTNO is set to the first depratment value as present in the LOV department table we loaded earlier. Also the corresponding filename to be generated is set to the variable $FILENAME. So with each iteration of the WHILE loop it will set the department for which it has to extract the data from the employee table and generate the corresponding department file.
Script to set Department
Script to set Department
  • Next we go to the final dataflow which will extract the data from the EMP table and generate flatfiles for each department. Before that in order to pass the variable values we set earlier to the dataflow, we define the following Input Parameters at the Dataflow level and also assign the variable values to these parameters at Workflow level.
Define Parameters
Define Parameters
Assign Parameters values
Assign Parameters values
  • Now the final dataflow looks like below. We have the EMP table as source followed by a Query transform and lastly the flat file target.
Dataflow to generate files
Dataflow to generate files
  • In the Query transform we filter the employee records for the department number as set in the Dataflow Parameter. Also additionally we generate a sequence or serial number of the employees in each of the flat files.
QUERY transform to filter DEPTNO
QUERY transform to filter DEPTNO
  • Lastly we place the file-format of the generated employee file for each department. Note we need to set the File name property of the file-format to the dataflow Parameter we assigned earlier.
Target Flat file Format
Target Flat file Format

We are done with our first requirement.

Requirement 2: Now we try the reverse scenario i.e. load the database employee table with the information in the form of flatfiles received from source systems. These flatfiles have employee information based on each department; Also the number of files or departments is not know in Advance. Note, that the part of the flatfile name bears the dept information that also need to be mapped to deptno column of the employee table.

Here we go:

  • We will design a dataflow to read all the input flatfiles having employee information extract the department number information from the filename and populate to the target relational table. Below is how the dataflow looks like:
Dataflow to read multiple Flat file Format
Dataflow to read multiple Flat file Format
  • First of all we place the fileformat as our source. Note the filename is using wild character search. i.e. DEPT*.txt; which means it will read all files in the given root directory having filename starting with DEPT with extension .txt. Next the Include file name column property of the Source Information of the file format is set to Yes with column name as FILENAME. This will help to read the filename information and pass in the dataflow transform.
Source Flat file Format
Source Flat file Format
Source Information Flatfile Format
Source Information Flatfile Format
  • Next we use the Query Transform to format the Dataset. We will extract the department number from the filename itself, as shown below:
Query Format
Query Format
  • Lastly we place the Target Template table.
Target Template Table
Target Template Table

Hence our second requirement is also done.