Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Including the filename in a bcp load. Expand / Collapse
Author
Message
Posted Friday, February 7, 2014 2:56 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, November 24, 2014 12:40 AM
Points: 652, Visits: 1,894
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.
Post #1539058
Posted Friday, February 7, 2014 2:34 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:20 AM
Points: 12,927, Visits: 32,332
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1539406
Posted Monday, February 10, 2014 7:11 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, November 24, 2014 12:40 AM
Points: 652, Visits: 1,894
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.
Post #1539748
Posted Monday, February 10, 2014 7:13 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, November 24, 2014 12:40 AM
Points: 652, Visits: 1,894
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!
Post #1539750
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse