Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Including the filename in a bcp load.


Including the filename in a bcp load.

Author
Message
David McKinney
David McKinney
Right there with Babe
Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)

Group: General Forum Members
Points: 769 Visits: 2090
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.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14913 Visits: 38903
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!

David McKinney
David McKinney
Right there with Babe
Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)

Group: General Forum Members
Points: 769 Visits: 2090
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.
David McKinney
David McKinney
Right there with Babe
Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)

Group: General Forum Members
Points: 769 Visits: 2090
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search