How to Export SQL Server Data to a Text File Format?
Last Updated :
15 Jun, 2024
Exporting SQL Server data to a text file is a common task that is used in data migration, data sharing, etc. SQL Server provides several methods to export data to a text file format, including using the SQL Server Management Studio (SSMS), the SQL Server Command Line Tool (sqlcmd), and the SQL Server Integration Services (SSIS).
In this article, we will see how to export SQL Server data to a text file using these three techniques.
Before we proceed, let's set up our database and table.
Query:
CREATE DATABASE geeks;
USE geeks;
CREATE TABLE brands(
brand_id INT PRIMARY KEY,
brand_name VARCHAR(30) NOT NULL
);
INSERT INTO brands
VALUES
(1, 'Electra'),
(2, 'Haro'),
(3, 'Heller'),
(4, 'Pure Cycles'),
(5, 'Ritchey'),
(6, 'Strider'),
(7, 'Sun Bicycles'),
(8, 'Surly'),
(9, 'Trek');
The table will created, and now we will export this table into text file.
How to Export Table Data to a Text File in SQL Server?
There are three methods to export the table data from SQL Server into a text file:
- Saving Result to File via SSMS
- Using Import/Export Wizard in SSMS
- SQLCMD Utility
Let's understand each of these methods in detail. We will cover each method and learn how to export table data to a text file in SQL Server with examples and step-by-step process.
Method 1: Saving Result to File via SSMS
Saving result to file via SSMS is a simple way to export table data to a text file in SQL Server. This method involves selecting the query results and then saving them to a file.
Let's look at the steps for this method.
Step 1: First, let's have a look at our brand's table.
Query:
SELECT * FROM brands;

Step 2: Write down the query onto the editor whose output needs to be saved. If you want to save the results in a flat file, you can do this in SSMS. Right Click on Editor > Results to > Results to File:
Query:
SELECT TOP (1000)
[brand_id],
[brand_name]
FROM
[sample].[production].[brands];

Step 3: Execute the query. An option to specify the name and path will be displayed. Change the type to All Files and Save it with the .txt extension:

Step 4: Result.txt file looks like this:

Method 2: Using Import/Export Wizard in SSMS
The SQL Server Import and Export Wizard is a tool in SQL Server Management Studio (SSMS) that allows users to copy data from one location to another. Using this tool, we can export the data to text file and save it for later use.
Let's look at the steps to perform this method.
Step 1: When we right-click a database in SSMS. It is possible to import or export data. Navigate to Tasks>Export Data:

Step 2:Â The SQL Server Import and Export wizard will be launched. We will export from SQL Server to a Flat file. Select the SQL Server Native Client 11.0 as the Data Source:

If necessary, specify the Server name and connection information:

Step 3:Â Select Flat File Destination from the destination drop-down menu and hit Browse to set the file name and path:

Step 4:Â The flat file name in our case would be Result.txt:

Step 5:Â Once we have determined the file name and path, proceed as follows:

Step 6:Â Choose "Copy data from one or more table or views" or select second option to specify our own query:

Step 7:Â To export the data instantly, choose Run immediately:

Step 8:Â The Result.txt file will contain the output:

Method 3: SQLCMD Utility
The SQL Server Command Line tool is SQLCMD. This tool allows you to store the results in a file. When utilizing batch files to automate processes, this option comes in handy.
Let's check the steps to use this method.
Step 1: Here's how our SaveOutputToText.sql file look's like:
Query:
SELECT TOP (1000)
[brand_id],
[brand_name]
FROM
[sample].[production].[brands];
Step 2: Use the following command on your terminal to save the results of any query onto file:
Query:
sqlcmd -i SaveOutputToText.sql -o Result.txt
Step 3: The Result.txt file contains the output:

These were the best three methods to export data to text format in SQL Server. You can use any of the three methods provided as all of them are easy and effective.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read