- Introduction
-
Table of Contents
- Microsoft SQL Server Defined
- Microsoft SQL Server Features
- Microsoft SQL Server Administration
- Microsoft SQL Server Programming
- Performance Tuning
-
Practical Applications
- Choosing the Back End
- The DBA's Toolbox, Part 1
- The DBA's Toolbox, Part 2
- Scripting Solutions for SQL Server
- Building a SQL Server Lab
- Using Graphics Files with SQL Server
- Enterprise Resource Planning
- Customer Relationship Management (CRM)
- Building a Reporting Data Server
- Building a Database Documenter, Part 1
- Building a Database Documenter, Part 2
- Data Management Objects
- Data Management Objects: The Server Object
- Data Management Objects: Server Object Methods
- Data Management Objects: Collections and the Database Object
- Data Management Objects: Database Information
- Data Management Objects: Database Control
- Data Management Objects: Database Maintenance
- Data Management Objects: Logging the Process
- Data Management Objects: Running SQL Statements
- Data Management Objects: Multiple Row Returns
- Data Management Objects: Other Database Objects
- Data Management Objects: Security
- Data Management Objects: Scripting
- Powershell and SQL Server - Overview
- PowerShell and SQL Server - Objects and Providers
- Powershell and SQL Server - A Script Framework
- Powershell and SQL Server - Logging the Process
- Powershell and SQL Server - Reading a Control File
- Powershell and SQL Server - SQL Server Access
- Powershell and SQL Server - Web Pages from a SQL Query
- Powershell and SQL Server - Scrubbing the Event Logs
- SQL Server 2008 PowerShell Provider
- SQL Server I/O: Importing and Exporting Data
- SQL Server I/O: XML in Database Terms
- SQL Server I/O: Creating XML Output
- SQL Server I/O: Reading XML Documents
- SQL Server I/O: Using XML Control Mechanisms
- SQL Server I/O: Creating Hierarchies
- SQL Server I/O: Using HTTP with SQL Server XML
- SQL Server I/O: Using HTTP with SQL Server XML Templates
- SQL Server I/O: Remote Queries
- SQL Server I/O: Working with Text Files
- Using Microsoft SQL Server on Handheld Devices
- Front-Ends 101: Microsoft Access
- Comparing Two SQL Server Databases
- English Query - Part 1
- English Query - Part 2
- English Query - Part 3
- English Query - Part 4
- English Query - Part 5
- RSS Feeds from SQL Server
- Using SQL Server Agent to Monitor Backups
- Reporting Services - Creating a Maintenance Report
- SQL Server Chargeback Strategies, Part 1
- SQL Server Chargeback Strategies, Part 2
- SQL Server Replication Example
- Creating a Master Agent and Alert Server
- The SQL Server Central Management System: Definition
- The SQL Server Central Management System: Base Tables
- The SQL Server Central Management System: Execution of Server Information (Part 1)
- The SQL Server Central Management System: Execution of Server Information (Part 2)
- The SQL Server Central Management System: Collecting Performance Metrics
- The SQL Server Central Management System: Centralizing Agent Jobs, Events and Scripts
- The SQL Server Central Management System: Reporting the Data and Project Summary
- Time Tracking for SQL Server Operations
- Migrating Departmental Data Stores to SQL Server
- Migrating Departmental Data Stores to SQL Server: Model the System
- Migrating Departmental Data Stores to SQL Server: Model the System, Continued
- Migrating Departmental Data Stores to SQL Server: Decide on the Destination
- Migrating Departmental Data Stores to SQL Server: Design the ETL
- Migrating Departmental Data Stores to SQL Server: Design the ETL, Continued
- Migrating Departmental Data Stores to SQL Server: Attach the Front End, Test, and Monitor
- Tracking SQL Server Timed Events, Part 1
- Tracking SQL Server Timed Events, Part 2
- Patterns and Practices for the Data Professional
- Managing Vendor Databases
- Consolidation Options
- Connecting to a SQL Azure Database from Microsoft Access
- SharePoint 2007 and SQL Server, Part One
- SharePoint 2007 and SQL Server, Part Two
- SharePoint 2007 and SQL Server, Part Three
- Querying Multiple Data Sources from a Single Location (Distributed Queries)
- Importing and Exporting Data for SQL Azure
- Working on Distributed Teams
- Professional Development
- Application Architecture Assessments
- Business Intelligence
- Tips and Troubleshooting
- Additional Resources
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' GO -- Now read it SELECT * FROM #textfile -- And then clean up DROP TABLE #textfile GO
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.
