Dynamically create and populate staging tables from CSV files

  • Hi Stan, I very much like the article.

    Sometimes I just want to ram some data into a database simply to explore the data. This technique can be very helpful when that is my aim.

    Nice work.

    John R

  • jimbobmcgee (1/10/2012)


    That said, I do want to offer some pointers with the .NET code itself, which I feel would make this better as a teaching article or reusable code block:

    [enumerated list 1-14 of constructive feedback]

    Thank you for the additional insights. If the original is a solution-starter, your comments are a reminder to keep a quality end in mind during development. Since we rarely go back to improve these kinds of utilities, it's a good idea to get them as near-optimal as possible on the first effort. (time permitting)

  • That's some really nice code.

    I just wrote mine to show that the idea would work.

    Yours would work well.

    Thanks!

  • @bhavjay

    To me, a staging table is just that, the first stage of processing the data.

    Since we don't really know what kind of data the fields are going to hold, we poke the data into VARCHARs just to get it into a table.

    The next stage would be actually looking at the data and writing an insert into/select statement that would cast the fields to the desired types and insert the records into the destination table.

  • Jeff Moden (1/10/2012)

    Of course, you could also pre-process tables before they come anywhere near SQL Server but I've found that writing such code is a bit of a fright because you might not have set based, declarative tools such as are available in SQL Server to keep from having to write read/write routines with lots of loops and decisions on your own.

    Actually, performing loops and tests in other languages is much easier. T-SQL is not that great a language in regards to either performance or feature sets. T-SQL's main advantage is that it has a database system hooked up to it.

    I don't think you should give up on loops and tests just because T-SQL is not that great a programming environment, unless you're really just not that interested in programming in general.

    /opinion

  • Actually, performing loops and tests in other languages is much easier. T-SQL is not that great a language in regards to either performance or feature sets. T-SQL's main advantage is that it has a database system hooked up to it.

    I don't think you should give up on loops and tests just because T-SQL is not that great a programming environment, unless you're really just not that interested in programming in general.

    /opinion

    No TSQL is not the best language for performing loops. But there are few scenarios in etl, where loops are the best algorithm to process data. The transformations after staging fore me are typical exercises of set-based processing.

  • Jeff Moden (1/10/2012)


    Stan,

    Nice step-by-step article with some timely graphics to show how to do things. Although it wasn't specifically named as such, I believe that folks will come to realize that this is a very good "spackle" article that demonstrates the beginnings of a technique. Like any "spackle" article, it's not meant to be a complete solution but it'll sure get some thought juices going for people who have ever had to import "unknown number of columns" files like I've had to do in the past. You could have saved yourself a bit of commentary by identifying that the popups where there only for verification during development and, perhaps, more clearly identifying not only when this technique might be used along with suggested "next steps", but it's a good article. Thanks for taking the time to write it and share it.

    Sorry Jeff - couldn't help laughing at this post. Not sure if you are aware of the slang "spackle": urban dictionary

    :hehe:

    Poor Stan

  • This is good to generate an initial cut of the SQL code that I will then use at the start of my SSIS package to create the staging tables if not present, Of course for that I do not need the data loading part. I would use a dataflow task to do the actual loading.

    Saves me some typing and hence typos.

  • Thank you for the article. For .NET-challenged SQL developers like myself this provides a great starting point to go on and adapt this approach for our specific needs. Also thank you to everybody who contributed subsequent enhancements. You are a great bunch!

  • surreydude. (1/11/2012)


    Jeff Moden (1/10/2012)


    Stan,

    Nice step-by-step article with some timely graphics to show how to do things. Although it wasn't specifically named as such, I believe that folks will come to realize that this is a very good "spackle" article that demonstrates the beginnings of a technique. Like any "spackle" article, it's not meant to be a complete solution but it'll sure get some thought juices going for people who have ever had to import "unknown number of columns" files like I've had to do in the past. You could have saved yourself a bit of commentary by identifying that the popups where there only for verification during development and, perhaps, more clearly identifying not only when this technique might be used along with suggested "next steps", but it's a good article. Thanks for taking the time to write it and share it.

    Sorry Jeff - couldn't help laughing at this post. Not sure if you are aware of the slang "spackle": urban dictionary

    :hehe:

    Poor Stan

    Now THAT's funny. 😀 But, no... not the type of spackle I was referring to.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • patrickmcginnis59 (1/10/2012)


    Jeff Moden (1/10/2012)

    Of course, you could also pre-process tables before they come anywhere near SQL Server but I've found that writing such code is a bit of a fright because you might not have set based, declarative tools such as are available in SQL Server to keep from having to write read/write routines with lots of loops and decisions on your own.

    Actually, performing loops and tests in other languages is much easier. T-SQL is not that great a language in regards to either performance or feature sets. T-SQL's main advantage is that it has a database system hooked up to it.

    I don't think you should give up on loops and tests just because T-SQL is not that great a programming environment, unless you're really just not that interested in programming in general.

    /opinion

    I'll have to resort to the "It Depends" aspect. If you want to check all data in a "field", it's a whole lot easier to do a test for it in a column in T-SQL than it is to write code to cycle through every row of that "field". Because T-SQL will use the very low-level "Pseudo Cursors" behind the scenes, it'll do it all at the same or almost the same speeds as writing an external validation script.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Although I don't particularly care for SSIS nor the fact that the technique in this article had to use a script to do the job (I did my thing 100% in T-SQL with no problems), the article is a reasonable introduction as to how you might begin to approach such a thing in SSIS.

    Jeff,

    I for one would be interested in getting eyes on the T-SQL solution you mention!

    SJT

  • Can Please any1 tell me what if we really want to know the data types of the columns and create the table accordingly. For small no. of files we can even do this thing 1 by 1 . But if the files are in 1000 s then what should be the proper solution to built the staging database. Please suggest some 1 its urgently required.

    Can I use any .dll file and modify it accodingly to get the requirement done.

  • Hi All,

    Can I use this with excel files ??

  • Reading Excel files is not trivial. You can export them to CSV files out of Excel. CSV files will work fine.

    You would have to convert them manually one-at-a-time, unless you purchase a third-party application like the one at this link:

    http://www.softinterface.com/Convert-XLS/Features/Convert-XLSX-To-CSV.htm

Viewing 15 posts - 16 through 30 (of 38 total)

You must be logged in to reply to this topic. Login to reply