Solved

Comments in dat file for BCP import

Posted on 2008-10-22
7
261 Views
Last Modified: 2013-11-30
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
Comment
Question by:brettr
  • 3
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22787933
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
 

Author Comment

by:brettr
ID: 22788166
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
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22788312
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:brettr
ID: 22788958
Sorry.  I don't know how better to explain it than the example I gave.  Where are you confused?
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 22981983
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
 

Author Closing Comment

by:brettr
ID: 31508843
Good call with find /v "//" mydata.dat >> myimport_data.dat and additional sql syntax.  Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now