Including the filename in a bcp load.

  • I'm loading a bunch of csv-type files via bcp using an xml format file, and triggering the load from a batch file on the command line.

    My batch file looks like this (only much bigger.)

    bcp sandbox.dbo.ima in LC07101_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07201_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07301_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07401_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07501_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07601_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07701_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    bcp sandbox.dbo.ima in LC07801_returnindex.txt -T -S L8653 -f imaformat.xml -F 2

    I can load the data into my table ok, but I'd like to know which file each row was sourced from. Is there a way to do this with bcp? (I can change the batch file to pass in the filename if that's possible.)

    Thanks guys,

    David.

  • David, where would you put the data reference to the file? is there an additional column at the end of the table, that you can use to update right after the BCP;

    for example, if the destination table has 15 columns, and bcp inserts into the first 14 columns, afterwards you could call sqlcmd to update the destination table where col15 is null;

    so you'd have to add a sqlcmd line for each file processed to your script file.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi Lowell,

    Thanks for this idea. I did try almost exactly this, but I was using osql. For whatever reason, I couldn't get the osql call to work. I hadn't thought of sqlcmd, which is dumb because I use it all the time in SSMS. As I was in a big hurry, I just knocked up a quick SSIS package, and did it like that.

    Cheers,

    David.

  • Lowell (2/7/2014)


    David, where would you put the data reference to the file? is there an additional column at the end of the table, that you can use to update right after the BCP;

    for example, if the destination table has 15 columns, and bcp inserts into the first 14 columns, afterwards you could call sqlcmd to update the destination table where col15 is null;

    so you'd have to add a sqlcmd line for each file processed to your script file.

    Now that I read my initial post, I realised I could have been much clearer. In any case, you worked out what I meant. Thanks!

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

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