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.
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.
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!
After 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.
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.
Hit OK, to save the step and continue.
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.
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.
Also, 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.
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.
Next 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.
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!