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
How to Export SQL Server Data to a CSV File?
Here we will see, how to export SQL Server Data to CSV file by using the 'Import and Export wizard' of SQL Server Management Studio (SSMS). CSV (Comma-separated values): It is a file that consists of plain text data in which data is separated using comma(,). It is also known as Comma Delimited Files
2 min read
How to Export Data From SQL Server to Flat File in SSIS?
In this article, we will learn how to Export Data From SQL Server To Flat File in SSIS. For this, we are going to create a package to select the data from the SQL server database and export SQL Server table data to the Flat File in the local drive. Prerequisites:Make sure Visual Studio 2019 is insta
3 min read
Export SQL Server Data From Table to CSV File
SQL Server is a very popular relational database because of its versatility in exporting data in Excel, CSV, and JSON formats. This feature helps with the portability of data across multiple databases. Here, we will learn how to export SQL Server Data from a table to a CSV file. Tools like Azure Dat
3 min read
How to Export a Table Data to a PDF File in SQL?
SQL Server is a versatile database. It is used across many industries. We can use AZURE data studio as well as SQL Server Management Studio for doing various operations (DDL, DML, Stored Procedure, Trigger preparations) etc., SQL Server supports the portability of data using the EXPORT option. By de
2 min read
How to Import and Export SQL Server Data to an Excel File?
SQL Server is very popular in Relational Database and it is used across many software industries. Portability of data is a much-required feature of any database. i.e. Database should support features like exporting database data to Excel/CSV/JSON and also should import data from them. In this articl
3 min read
How to Export Data to the .CSV File Using SQL Server Stored Procedure?
Exporting data from SQL Server to a CSV file is a common task when handling large datasets or sharing data with other applications. SQL Server Management Studio (SSMS) provides a straightforward way to export tables using its Import and Export Wizard. In this article, we will see, the process of exp
3 min read
How to Export Data from SQL Server to MySQL through SSIS?
SQL Server Integration Services (SSIS) is a widely-known tool for data migration, transformation, and integration. A widespread example of an SSIS application is that of exporting data from an SQL Server database to a MySQL database. Although this process is often complicated, it is possible to simp
4 min read
How to Import and Export SQL Server Database?
Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
3 min read
How to Read Data From Text File in Excel VBA
VBA Program to read a Text file line by line (Sales Data) and place it on a worksheet It provides a concise introduction to the process of importing data from text files into Microsoft Excel using Visual Basic for Applications (VBA). This introductory article serves as a starting point for individua
5 min read
How to export dataframe to RDATA file in R?
In this, article we are going to save the information of a data frame in an RDATA file and display the information of the file using R Programming language. To save the information of a data frame in a file and display the information of the file in R language is as follows: Using the save function
3 min read