If you want to export a large SQL server query to a txt file, there are more than one ways. Which one to choose depends on your preference and credentials available to you. In this article, we explain five ways you can achieve this.
Five Ways to Export SQL Server Query Results to txt file
Here are the five different methods. Review them and proceed with the one that best suits your skills and preference:
This command will store the sqlquery.sql file as sqlqueryoutput.txt file, thus achieving the intended goal.
Here, the Invoke-Sqlcmd will the sqlquery.sql and store the output in sqlqueryoutput.txt file.
Open the BCP tool and then execute the following:
The code will direct BCP to export query results to the bcpoutput.txt file. -T is used to indicate that we’re using a Trusted Connection, while -c implies the operation of data type.
If you have millions of rows in your SQL table, using BCP is the fastest method.
Here are the steps involved:
So those were the five ways for carrying out this operation. Go with the one that you find the easiest. In case a particular process didn’t work, try another or contact the hosting support team.
Five Ways to Export SQL Server Query Results to txt file
Here are the five different methods. Review them and proceed with the one that best suits your skills and preference:
- Using the SQL Server Command Line
Code:
sqlcmd -i c:\sql\sqlquery.sql -o c:\sql\sqlqueryoutput.txt
- Using PowerShell
Code:
Invoke-Sqlcmd -InputFile "C:\sql\sqlquery.sql" | Out-File -filePath "C:\sql\sqlqueryoutput.txt"
- Using the Import/Export Wizard
- Right-click on the database inside SSMS on which you want to work
- From the list, select Import or Export data
- Go to Export Data under Tasks
- Open SQL Server Import and Export wizard
- Select Microsoft OLE DB Provider as the Data Source (this may require you to specify both the connection info and server name)
- Then from the Destination section, click on Flat File Destination
- Input the file name and path (where you want to save the file) at the required fields and then click on Next
- Select Write a query to specify the data to transfer (in this section you’ll have to mention the SQL Server Query and then select Parse in order to verify the query)
- Lastly, tap on Run Immediately to start the process of exporting the result to a text file
- Using BCP
Open the BCP tool and then execute the following:
Code:
bcp "SELECT TOP 10[PatientEntityID],[NationalIDNumber],[DepartmentNode], FROM Patient2021CZT2.[HumanResources].[Patient] WITH (NOLOCK)" queryout c:\sql\bcpoutput.txt -c -T
If you have millions of rows in your SQL table, using BCP is the fastest method.
- Using SQL Server Management Studio
Here are the steps involved:
- Open the SQL Server Management Studio
- Navigate into Tools > Option
- Then navigate into Query Results > SQL Server
- Here, select Resultsto file (in this step you’d need to define the default location for saving the SQL Server query results)
- Finally, create a query and execute it. You’ll be asked to provide a name and filetype of the file where the output will be saved.
So those were the five ways for carrying out this operation. Go with the one that you find the easiest. In case a particular process didn’t work, try another or contact the hosting support team.