Introduction
In our previous blog we saw How to write data into CSV file in SSIS (GZip / Split). Now in this blog, we will see How to Pivot CSV Data in SSIS using CSV Source. It also supports Pivot mode so you can convert single CSV string value into Rows. In this article we also see how make some CSV columns as parent and not consider it in pivot and load it as it is using JSON Generator and JSON Parser Transform Pivot Option.
In nutshell, this post will focus on how to Pivot CSV Data into multiple columns and rows.
Prerequisites
Before we perform the steps listed in this article, you will need to make sure the following prerequisites are met:- SSIS designer installed. Sometimes it is referred to as BIDS or SSDT (download it from the Microsoft site).
- Basic knowledge of SSIS package development using Microsoft SQL Server Integration Services.
- Make sure ZappySys SSIS PowerPack is installed (download it, if you haven't already).
- (Optional step). Read this article, if you are planning to deploy packages to a server and schedule their execution later.
How to Pivot CSV Data in SSIS using CSV File Source
Let´s start with an example. In this SSIS CSV Parser Transform example, we will Parse database column string into multiple columns and rows.
- First of All, Drag and drop Data Flow Task from SSIS Toolbox and double click it to edit.
- Furthermore, drag and drop the CSV Source and Double click on it to configure it.
- Enter the CSV file path and select desire column delimiter most common in CSV is comma and check the First Row as headers and click on preview.
- Now, go to Pivot Columns to Rows Tab and checked Enable Pivoting and click on Preview Data to view the data.
- That’s it we see how to read data from CSV File and also Pivot the CSV Data using CSV Source.
- Now In the next section we will see how to how make some CSV columns as parent and not consider it in pivot and load it as it is.
How make some CSV columns as parent.
Sometimes you just need to make child columns in to rows and want to leave parent level columns as it is.
- First of All, Drag and drop Data Flow Task from SSIS Toolbox and double click it to edit.
- Secondly, drag and drop the CSV Source and Double click on it to configure it.
- Furthermore, enter the file path and click on preview.
- For example if we want to pivot Email and Phone only in pivoting and wants to exclude parent level columns FirstName and LastName as it is.
- For that drag and drop JSON Generator Transform and Select Output Mode as Output One JSON Per Parent Input Row and Add Unbound Nested Element and named it as Persons and click on OK.
- Now add that two parent level fields FirstName and LastName in the Persons.
- Now select Person Element and right click and Add Unbound Nested Element and named it as Contacts and click on OK.
- Now add that two child level fields Email and Phone in the Contacts.
- Therefore JSON Generator Transform looks like below screen. Now Copy that Sample Preview JSON String to use it in the JSON Parser Transom as sample string.
- Similarly, drag and drop JSON Parser Transform and select the Input JSON Column and Paste that copied Sample JSON String and select the Contacts filter to read the Child data and click on Include Parent.
- Now go to Pivot Columns to Rows Tab and checked the Enable Pivoting and click on the Preview.
- Finally, we are ready to load this data.
- Let’s load this data in the SQL Server.
Load Pivoted CSV File data into SQL Server
- Inside Data Flow, Drag and drop Upsert Destination Component from SSIS Toolbox
- Connect our Source component to Upsert Destination
- Double click Upsert Destination to configure it
- Select Target Connection or click NEW to create new connection Configure SSIS Upsert Destination Connection - Loading data (REST / SOAP / JSON / XML /CSV) into SQL Server or other target using SSIS
- Select Target Table or click NEW to create new table based on source columns
- Click on Mappings Tab to Auto map columns by name. You can change mappings as you need SSIS Upsert Destination - Columns Mappings
- Click OK to Save Upsert Destination Settings
- That's it, You are now ready to run data flow. NOTE: If you wish to debug data flow and see records when you run, add data viewer by right click on blue arrow > Click Enable Data Viewer
- To execute data flow, Right click anywhere inside Data Flow Surface and click Execute Task
Conclusion
After all, we saw you how to read CSV file data, pivot the data and Similarly, using SSIS JSON Generator and Parser Transform Pivot the CSV Data excluding some columns from pivoting. To explore many other scenarios not discussed in this article download SSIS PowerPack from here (includes 70+ Components).
References
Finally, you can use the following URL for more information.
- Help File: CSV Source