Export records from SqlServer to Text file

The fastest way to export records form SQL Server table to a text file is to use BCP command.

Step 1: First step will be to enable XP_CMDSHELL.


Use Master
GO

EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
GO

EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
GO



Query Output:

Configuration option 'xp_cmdshell' changed from 1 to 0. Run the RECONFIGURE statement to install.
Configuration option 'show advanced options' changed from 1 to 0. Run the RECONFIGURE statement to install.



Step 2: Execute the following given query below

EXEC xp_cmdshell 'bcp "SELECT ColumnName FROM [DataBaseName].[Schemas].[TableName]" queryout "C:\Temp\CountryRegion.txt" -T -c -t,'

Comments

Popular posts from this blog

SQL Interview Questions and Answers

Generic Interface and Factory Pattern in C#

How to get device info IMEI programmatically in xamarin android