Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Using Variables in SSIS: Purpose review and functional examplesThe Good, the Bad and the Ugly of Database Design »
    comments

    The Script Task

    The script task is pretty much the all mighty in terms of what you can do. When you sit there initially with the script task in front of you, you are going to be saying you can write your entire process in this thing. I mean in reality you could. You could open a connection to your source database and start hacking your way to the results, the power is there! It has the ability for you to write .NET code in its best form (or worst) and run that task to your SQL Agents heart’s content.

    Alright, stop getting excited one task man, there is a place and a job for everything. SSIS has gifted abilities to give you options. I probably could have written the 6 ways to import data completely from SSIS and still had the 6 completely different methods. That doesn’t mean I should do it any certain way just because I can. That has always been the foundation of multiple platforms, languages and frameworks. Each has the ability to perform certain jobs in certain situations better than the next.

    A real world scenario

    It is very common for the need to extract data from your databases in order to transform and import into flat files. Many database developers new so SSIS attach themselves to the script task to complete this type of process. System.IO is exposed as many namespaces in the script task so you can create the new file dynamically and then pump the data into the file while in the same context. The script task will be more than happy to do this for you and possibly in many cases efficiently. The problem is at this point you have just turned all of SSIS into a container around one or multiple simple executables. What you have in SSIS with the script task at this point is simply a container of the executable. A major reason I’m picking on the new flat file creation and export from a database source is I’ve seen it many times over. When I ask the developers why they do this, they usually explain to me how they selected a file for the flat-file connection and the OK button greyed out, leaving them unable to continue, so they figured it was broken or not capable of the task. "Don't stop there!", I tell them, "You are on the right track".

    The flat-file connection requires you to map your columns, but this is not obvious and is commonly missed. You can take the flat file connection and set the columns to the way they will be on export. This means the mappings will match up on the later setup of the transform from the source.

    Let’s give it a try

    Right click Connection Managers and hit New Flat File Connection.

    Enter in some funky name so you remember this was an example and you can go back to it when you need to refresh your memory.

    Enter a path, any path. I like littering my C drive with files and in a month I’ll be wondering if I should or shouldn’t delete.

    Now go into Advanced. Note: The OK is still grayed out. Click the New column button a few times

    Ah…I can save it now

    Let's try it out with a test data flow I created while helping someone out a few weeks ago. Change your connection manager property in a flat file destination to point to our new file connection.

    Map some columns over

    And let’s execute to see the results.

    :-) It’s a beautiful thing we have. Checking our file that was created on the C drive we see the contents pumped in

    Comparing and wrapping up

    Now if you did this in a script task, you would first have to create variables to hold the file locations, then create the file in the location and then go about setting all the properties of the file connection along with the file destination.
    That would be one way.

    The point I’m trying to get across is that the script task is nothing short of a powerful tool. I have used it to do things in the new ETL platform for SQL Server that I couldn’t dream of doing in DTS. Basically the lack of the script task and level of progamming in DTS would force me to start writing external code in a service or executable. This in some cases you can do with the script task alone. My career started in development so that was a way I turned quickly early on in my SSIS trials. I knew that wasn’t the greatest thing though. It brings up maintainability, knowledge transfer issues, documentation time and points of failure to name a few.

    So the next time you go to perform a task in SSIS and that script task stares you in the face telling you he can do it


    ALL NIGHT LONG!!!!!

    You may want to think about it and look around to see if SSIS already has something that prevents the code from being rewritten allowing you to take advantage of SSIS and the abilities it brings with it.

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt
    Social SitingsTwitterLinkedInLTD RSS Feed
    2524 views
    Instapaper

    2 comments

    Comment from: sqlsister [Member] Email
    sqlsister Well personally I also prefer to use the actual intended column names instead of column0 etc. They map easier to the source as well.

    A gotcha that we ran into recently was that we had an export that created 11 export flat files. The developer copied the flat file connections and changed the file name. So when we ran the package, each one had the same number of columns (at least he started with the one with the most columns) as the first one with no data in most of the columns. Not fun. PlLease remember when copying flat file connection to adjust for the columns you need.
    12/16/09 @ 14:18
    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) :-) I probably should have named those better. Thanks for correcting that SQLSister
    12/16/09 @ 14:41

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)