Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL 301-ETL With SSIS, Part 3

Posted on January 21, 2009 by slowder

Ok, so in our SSIS package we currently have all our variables set up, we have a for each loop that will look in a certain folder for all files matching a certain file pattern.  Inside that loop we have a script that will build our archive path and error paths.  Next we need to prepare the raw table for import.

A raw table is a table that will take any input from your data source, as long as the number of columns match.  This method may not work best for you.  In my experience you’ll get files with errors such as bad dates passed to you in a date field, or you’ll get differing formats for telephone numbers.  That experience of getting “bad” files over and over led me to this design.  If you have more consistency in your files, feel free to use a more structured table for import, rather than my raw design.

If you haven’t cracked open the file we’re about to load, please do so now.

   1: NameStyle,FirstName,MiddleName,LastName,PasswordHash,PasswordSalt

   2: 0,Anakin,NULL,Skywalker,NO PASSWORD,NO SALT

   3: 0,Michael,Jason,Bender,NO PASSWORD,NO SALT

   4: 0,Gustavo,NULL,Achong,NO PASSWORD,NO SALT

   5: 0,Catherine,R.,Abel,NO PASSWORD,NO SALT

   6: 0,Starkiller,Biggs,Darklighter,NO PASSWORD,NO SALT

This is a really simple file, just six columns.  We’re going to import this file into Person.Contacts in the AdventureWorks database.  My usual method is to take the names of the columns the data will go into eventually and build a create table script using data type VARCHAR(255) for all of them, unless I know we need more (such as URLs,  I bump those to VARCHAR(1000).

This table isn’t built for any real optimization.  If you need to optimize your raw tables, we can talk about that later.  For now, we’re going for simple.  Here’s the raw table script I’m going to use.

   1: USE AdventureWorks

   2: GO

   3: CREATE TABLE Person.Contact_Raw (

   4:       NameStyle VARCHAR(255)

   5:     , FirstName VARCHAR(255)

   6:     , MiddleName VARCHAR(255)

   7:     , LastName VARCHAR(255)

   8:     , PasswordHash VARCHAR(255)

   9:     , PasswordSalt VARCHAR(255)

  10: )

Notice all the columns allow NULL.  That’s important because more times than not optional columns will be NULL.  So we need to be able to handle that!

etlSSIS_15After you’ve created your raw table, or you’re working from my training server and it already exists, we can continue our SSIS package.  Each time we process a file, we want to make sure the raw table is empty.  So we need to add a step that will run a T-SQL command.

In SSIS, that means you want the Execute SQL Task.  Click on it and drag it inside your Foreach loop, just below the Script task.

Let’s go ahead and wire this second step to the first.  If you click on your script task, you should see a green arrow sticking out from the bottom.  Click on this arrow, and drag it onto the Execute SQL Task you just added.

etlSSIS_16It should look something like the image to the right.

We’ll talk about some options you can use with the wiring later, when we deal with import failures. 

Now that our tasks are wired, let’s set up the Execute SQL task.  Double-click it to start the Execute SQL Task Editor.

The first thing we’ll do is name the task.  I’m going to name mine “2-Truncate Raw Table”.  Next, we’ll want to set the Connection.  Click just to the right of the word connection, and you’ll get a drop down list of all the database connections you’ve defined in your package.  Since we’ve only defined one (AdventureWorks), that’s the only one you can choose.  Choose it, and then we can set the SQLStatement.

You could either type the T-SQL into the line just to the right of “SQLStatement”, or you can click the […] button to the right of SQLStatement (You have to click on the line in order to see that button.) and you’ll get a multi-line editor.  I usually click the button to get that editor to type in my command.  Either way, enter the following script as your SQLCommand.

   1: TRUNCATE TABLE Person.Contact_Raw

Hit OK, to save the step and continue.

etlSSIS_17Now it’s time to do the bulk of the work in your SSIS Package.  We’re going to actually shuffle the data from our file to the raw table.  To do this we’re going to add a Data Flow Task.

Click the Data Flow Task option from your toolbox and drag it into your Foreach loop, just below the Truncate step. 

Go ahead and wire it up, drag the green arrow from the Truncate step to the Data Flow Task.

Like all the other tasks, we’ll start by setting the name for the Data Flow.  Right click it, and choose Properties.  The Properties tab will slide out from the right.  Look down the list and enter a useful name for this task.  I’m going with “3-Import Source to _Raw Table”.  After naming your Data Flow Task, double click on it to set it up.

I’d like you to notice when you double click on a Data Flow Task, your Visual Studio changes from the Control Flow view to the Data Flow view.

etlSSIS_18

The reason I point this out is, you need to realize your tab is changed, so when you’re finished setting up the data flow, you can return to the rest of your package on the Control Flow tab.

etlSSIS_19Also, check it out, the tool bar has changed.  You now see all the objects and tasks you can accomplish inside a data flow task.  Today we’re going to cover a simple flow of data from a Flat File Source to a OLE DB Destination.  But look at all the other options you have available to you.  That should get you thinking about what other tasks you could accomplish with SSIS Packages and specifically with Data Flow Tasks.

We’re going to start by dragging a Flat File Source object onto our Data Flow Task.  We need to configure this object to point to the NewFile Connection we created earlier.  That way, when the package runs, the changes we make to that connection will be passed on to this object.

Double Click the Flat File Source object and you’ll see the Flat File Source Editor.

etlSSIS_20

Since we only have one Flat File Connection defined, the editor picks that up automatically.  Click Preview to check out the demo file we’ve set the connection to.  Notice it picks up all the information we defined about the connection before.  The Columns have names, the data is populated nicely.  If you don’t see that in your preview, please contact me and let me know!

Feel free to explore the columns section and Error Output sections if you wish.  We’ll cover those sections more in a later post.

For now, you can click OK to continue.

etlSSIS_21Next we will define the OLE DB Destination.  Drag the OLE DB Destination object from your tool bar onto the Data Flow page.  Once you’ve done that, drag the green arrow from the Flat File Source object onto the OLE DB Destination object.  This tells the Data flow to take all the rows from the source to the destination.

After the arrow is connected, double click the OLE DB Destination object to continue.  You should see AdventureWorks already selected as your connection, since that’s the only OLE DB connection we defined.  You’ll have to select the table Person.Contact_Raw, so the data flow will know what table to insert all those records into.  For now, let’s leave the rest of the options alone.

etlSSIS_22Hit preview to check out how the table looks right now.  It should be empty, since we haven’t loaded any data into it.  If you have data already, please let me know.

Once you have all that set, click on Mappings to set up where each column of the source will be stored when it’s inserted to the destination.

Since we already defined the column names in the source, all the mappings are already defined.  NameStyle goes to NameStyle, FirstName goes to FirstName, etc.

If you hadn’t defined the column names already, you can define what column maps to what, by clicking each the Input Column for each row, and choosing which column to fill into the Destination Column.  You cannot change the destination columns, you can only change which input column to use.  You also cannot map the same column to more than one destination.  If you need to do that, you should use an Execute SQL Object to copy the values to the destination columns.

Once you’ve seen the mapping screen, click OK to continue.

You’ve now set up the data flow from the source file to the destination.  You could run your package right now, and it would import data from your flat file to the database.  There is a bit more to cover in this series.  I’d like to show you how to handle the transformation part of the ETL process. After that, I plan to cover handling errors in SSIS, and multiple paths in your Control Flow.  After that, I’ll branch out and cover more advanced topics such as logging and some of the other tools provided in the SSIS Toolbox.

Until then, if you have any questions, please send them in!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme