• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Comments in dat file for BCP import

I have a dat file that is imported into sql server via bcp.  It's format is:

1  33 this is text into a varchar() field
2  43 this is more text
3  44 here is some more text

I'd like do something like this:

1  33 this is text into a varchar() field
//this is part of question3 group
2  43 this is more text
//this is part of the question6 group
3  44 here is some more text

which allows people to look at the dat file data and realize what's going on.
0
brettr
Asked:
brettr
  • 3
  • 3
1 Solution
 
Mark WillsTopic AdvisorCommented:
Cannot do it in BCP, but might be able to do it via openrowset :

select * from OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=C:\mypath;','select * from  mytext.dat')

Then you can inspect each row and append a comment accordingly.

Can you share the file ? Or are you OK with the above ?
0
 
brettrAuthor Commented:
Don't want to append the comment.  It needs to already be in the file so someone can open it up and see comments.   Thanks.
0
 
Mark WillsTopic AdvisorCommented:
Sorry, misunderstood thought it was being added... so, you are saying there is variable record layouts in the dat file ? If not, then would you mind explaining what the problem is ? That technique above will handle variable inputs... Might like to see if it reads the dat file.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
brettrAuthor Commented:
Sorry.  I don't know how better to explain it than the example I gave.  Where are you confused?
0
 
Mark WillsTopic AdvisorCommented:
So, you haven't tried the import ?

Answer is still "no" it cannot handle embedded comments - it can be there, but you will have to code for it...

either import as single lines, then manually unpack the lines of interest (or delete the // prefixed lines), or write that back out to import just the lines of interest, or use a DOS command to strip out the lines that do not contain //  (e.g.  find /v "//" mydata.dat >> myimport_data.dat ) and then import that.

There are a number of possibilities to accommodate, it is just that BCP does not know about content other than what it is told as to how to read (such as with format files).

Can certainly use format files to skip columns, but not rows - somewhere it catches you out (e.g. ignore errors with maxerrors=999 - doesn't ignore in batch mode) e.g. can strip out comment lines :

select * from OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=C:\ee;','select * from  mytable.txt') where <column heading> not like '//%'

but the catch here is you have to do your own unpacking of the lines of interest...

So, I am not confused - just thought you wanted to add comments to begin with (which we sometimes want to do to flag any errors)...
0
 
brettrAuthor Commented:
Good call with find /v "//" mydata.dat >> myimport_data.dat and additional sql syntax.  Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now