Take each row from .txt file(s) and INSERT it into a SQL table

  • Hi,

    I am working on a project in which someone will drop up to 5 (.txt) files into a directory on a shared drive. I have to go in and pull each row from each (.txt) file and place it into a SQL table.

    Any assistance would be appreciated.

    Thanks

  • Have you tried using DTS? This would be a pretty simple task using the text file source connection.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • [font="Verdana"]Try Bulk Insert as well. Syntax:

    Bulk Insert {Table Name}

    From {File Name with complete path}

    With {Different Hints}

    For more information on Bulk Insert refer BOL and the below URL as well.

    http://msdn.microsoft.com/en-us/library/ms188365.aspx

    Mahesh

    [/font]

    MH-09-AM-8694

  • jalphor (5/13/2008)


    Hi,

    I am working on a project in which someone will drop up to 5 (.txt) files into a directory on a shared drive. I have to go in and pull each row from each (.txt) file and place it into a SQL table.

    Any assistance would be appreciated.

    Thanks

    Jalphor,

    If DTS isn't what you want, then Mahesh is correct... Bulk Insert will do... but it will need to be in the form of "dynamic" SQL to get the file name/path to change.

    To read file names in a given path, consider the following (try it)....

    EXEC Master.dbo.xp_DirTree 'C:\',1,1

    The "File" column should be pretty much self-explanatory. You can insert the output of that extended proc into a table using INSERT/EXEC.

    --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)

Viewing 4 posts - 1 through 4 (of 4 total)

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