Home > Articles > Data > SQL Server

SQL Server Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

SQL Server I/O: Working with Text Files

Last updated Mar 28, 2003.

It's amazing that the same letters that Gutenberg used in the first moveable-type printer are still in use today.

To encode text characters, a system called the American Standard Code for Information Interchange (ASCII) uses 7 bits (values from 1-127) for these letters, and the 8th bit is reserved for parity. When computers began to be used in other languages, extensions to these characters were created to allow their encoding, so Europeans could have a symbol for the Euro and the British pound.

The extensions weren't enough. Eventually, a new standard called Unicode was invented to encompass all languages in text format.

Groups of ASCII or Unicode characters make up what are commonly called "text files." Relational databases, such as SQL Server, don't natively work in this file format; instead, they use a more efficient binary proprietary file system.

From time to time, however, a text file is still the most universal method of data transfer. You should be familiar with SQL Server's means and limitations for working with them. In this tutorial, I explain the more common ways to work with text files.

Text files aren't just in one kind of format. The most common type of text file is one that was originally used for e-mail, called MIME - text/plain. The important thing to know about these files is that Macintosh (prior to OS X), UNIX, and Microsoft-based file formats that store these files might be slightly different. Therefore it's important to know the source of the files you'll work with. If you run across an issue when trying to parse a text file, check the source, and try to get it into a compatible format. There are lots of third-party programs that will do that for you.

Let's take a look first at exporting text files out of SQL Server, since that's the most popular application. If you just need a quick manual method to get data from SQL Server into a text file, you can use Query Analyzer, run a query, click in the bottom pane, and select File then Save As and select the Text File option.

You can also use Enterprise Manager, right-click a table, select Open Table and then Return All Rows. Select all the rows of data you're interested in, press CTRL-C, open Notepad and paste it in.

But you probably already knew you could do that. More than likely, you're looking for a more automated method to get data into text files.

The easiest way I've found to export text data from SQL Server is by using the osql utility. This utility is run from the command line of any computer with the SQL client tools installed, including the server. Run from the server, the command can be as simple as this:

osql -E -o c:\temp\authors.txt -d pubs -Q "SELECT * FROM authors"

The switches shown above are:

-E  Use trusted authentication
-o  Output file
-d  Database Name
-Q  Run a query and leave osql

If you're unfamiliar with the osql command, you can review my earlier work on this topic or just type:

osql -?

You can also use the command-line bulk copy program (bcp) to extract data into text files. In fact, bcp gives you quite a bit of control over formats, file layouts and such. Here's a sample bcp script that does the same thing as the previous example:

bcp pubs..authors out c:\temp\test.txt -S -U -P –c

I'll break this down a bit:

bcp  The bcp command
pubs..authors  The object to be exported
out  The direction – out is export and in is import
c:\temp\test.txt  The name of the file
-S –U –P  These set the server, user and password. Given this way they represent the current server and my current Windows account name and password
-c  Character, or text format

By far, the most popular method of exporting data is the Data Transfer Service, or DTS. You can access this program from Enterprise Manager by right-clicking a table or view and selecting All Tasks and then Export Data. Using this menu option choice starts a wizard that walks you through the entire process, and gives you the opportunity to save the "package" to edit or use again. I've covered the full process in an earlier tutorial, so refer to that for a screen-by-screen description.

Now for reading text files into SQL Server. You have many of the same options for importing data as you do for exporting, but you can use a trick or two in T-SQL to import data instead of using osql to export it. Here's a quick sample:

-- Create a table to hold the data
CREATE TABLE #textfile (line varchar(8000))
-- Read the text file into the temp table
BULK INSERT #textfile FROM 'c:\temp\test.txt'
-- Now read it
SELECT * FROM #textfile
-- And then clean up
DROP TABLE #textfile

The first part of this script creates a temporary table (you can use a permanent one if you wish) with one column – a varchar type with an 8,000 character limit.

The next line uses the BULK INSERT command to read the text file into the table, one line at a time. BULK INSERT looks for line breaks, so keep that in mind if you use this script.

The next lines just read the file and then drop the table; they're optional. You have that data available to you until you terminate the connection. Also, beware that temporary tables are created in the system database TempDB, so avoid using this script for large files.

Just as the bcp command has an out parameter, it also has an in parameter. Here's a simple example, importing data from a text file into a table that has the same shape as that file:

bcp pubs..authors in c:\temp\authors.txt -S -U -P –c

The advantage in using this command is that you have a lot more control over the import, even up to including a format file that can do some simple data molding on the way in.

If you're really after an industrial-grade, programmable method for doing data imports from a text file, Data Transformation Services is the way to go. As I mentioned earlier, you can start that process with a right-click and a Wizard, but if you really want to investigate the power of this application check out that other tutorial I pointed out.

Online Resources

Microsoft has a full rundown on Data Transformation Services in a white paper.

InformIT Tutorials and Sample Chapters

If you are going to use XML as the text file format for SQL Server, in addition to the articles I've referenced from my series make sure you check out John Griffin's book on SQL Server and XML, which includes this free chapter.