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

    « SQL Server and xp_cmdshell - The good, the bad and more uglyAutomating Index Statistics Collection with SSIS – The fast one »
    comments

    Many times I am given an Excel file, or have a data source that makes it easy to paste into Excel, and I need to use the data in my database for a one-time purpose of updating or checking other data. It's not worth creating an SSIS package, and it's not even worth using a wizard in SSMS. So generally I use bcp as a very fast method.

    To do that, I quickly create a staging table in my database, then save the Excel file as tab-delimited.

    Then I have to go play with bcp and figure out the syntax yet again, since I don't do it often enough to have made it unthinkingly automatic. Recently I got tired of these repeated lookups and created the following batch file to help out:

    @echo off
    :top
    if .%1==. goto end
    echo Ready to import file to SqlServerName, DB DBName:
    echo  %1
    set /P tablename=Type destination table name and press Enter:
    osql -S SqlServerName -d DBName
    -E -Q "truncate table %tablename%"
    bcp DBName.%tablename% in %1 -S SqlServername -T -c
    echo.
    shift
    goto top
    :end
    set tablename=
    pause

    Name the batch file "SqlServerName DBName import replace (drop file).bat".

    Now in Windows Explorer, just drag any file you want to import to your database onto the batch file. When prompted, type in the schema and table name you want it loaded to, and press Enter. You'll get a nice pause to see what the result was so you can fix it if not successful. It even supports dropping multiple files, and you'll be asked for a destination table name for each file.

    If you find yourself needing to load a text file to a particular table over and over, just make a batch file specific to that text file, and you can drag-and-drop your imports super easily.

    You can of course customize the batch file to do anything you want. To prompt for server name, database name, username, password, or anything you like. And you won't have to look up the syntax for bcp one more blasted time!

    About the Author

    Erik has been working in IT since 1993 and starting in 2004 has specialized in MS SQL Server query writing, database design, and reporting services. He also professionally does web site design/developing and writes C# applications (mostly for projects involving his databases and web sites). His career in the industry truly started when he first began using computers in the late 70s. In 1984 he began programming on the IBM PCJr, and performed game testing on King's Quest I. These early experiences gave him the taste and drive to continue an incremental and self-taught path all the way to his current position. Erik is also interested in taekwondo, go (ranks 4k on KGS), sci-fi books, mathematics, philosophy, religion, and rollerblading. He and his family live on the West Coast of the US.
    Social SitingsLTD RSS Feed
    450 views
    Instapaper

    1 comment

    Comment from: Erik [Member] Email
    Erik I modified the batch file code in the post to accept multiple files, all dragged and dropped as one.

    For posterity, here's the original code for single drops:

    @echo off
    echo Ready to import file to SqlServerName, DB DBName:
    echo %1
    set /P tablename=Type destination table name and press Enter:
    osql -S SqlServerName -d DBName -E -Q "truncate table %tablename%"
    bcp DBName.%tablename% in %1 -S SqlServerName -T -c
    set tablename=
    pause
    02/15/12 @ 16:43

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