Hi,
I have a table with some data in my sql database. I want to print the result of that table in to a file in my system from sql . I found some ways to print text from sql to a text format file. My requirement is to send the sql command output into a text file.Please suggest a way to solve my problem.

Naveen KumarRank #249 · 10K reputationPosted Oct 10, 2023, 7:56 AM EDT
If you are OK to use xp_cmdshell, then try below query.
EXEC xp_cmdshell 'bcp [database].[schema].[table] out "C:\Test\TableToFile.txt" -c -T'Ensure your server have configured to run xp_xmdshell, if not run the below query to configure it.
Use Master GO EXEC master.dbo.sp_configure 'show advanced options', 1 RECONFIGURE WITH OVERRIDE GO EXEC master.dbo.sp_configure 'xp_cmdshell', 1 RECONFIGURE WITH OVERRIDE GOJithu ThomasRank #437 · 5.7K reputationPosted Oct 2, 2023, 6:00 AM EDT
In SQL Server the
bcputility for exporting query results to a text file. You can execute it from SQL Server Management Studio or a command prompt. Here's an example:bcp "SELECT column1, column2 FROM your_table" queryout "C:\path\to\your\file.txt" -c -TReplace
column1, column2with the columns you want to export,C:\path\to\your\file.txtwith the path to your output file, andyour_tablewith the name of your table.Anu200 reputationPosted Oct 2, 2023, 6:46 AM EDT
Hi,
I tried the way which you suggested, but getting below error
Sachin SinghRank #66 · 26.2K reputationPosted Oct 4, 2023, 7:04 AM EDT
You can use the command Promt
where c:\sql\myquery.sql is the path of the SQL query file.