SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Export Data in Text File Using SQLCMD in SQL SERVER 2005

By Manish Mathur,

Introduction : Sql Server 2005 provides the new utility which is sqlcmd command utility. By using this utility we can execute the T-sql command as well as we can export the data in text file from any sql server table. SQLCMD using OLEDB provider to execute the T-Sql commands.

Script Description : This script comprises of one stored procedure script which have three input paramters and two output parameters as follows :-

@TableName -- To Get Table Name
@FilePath -- To Get Valid FilePath with FileName
@Separator -- To Get Field Separator
@Result -- To get error no if any error ocurred during execution
@ErrDesc -- To get error description if any error ocurred during execution

I have seen lot of examples on which data in exported in text file with field headers but field headers also containing the "-" characters in the text file but in this script i have removed these characters "-" from the exported text file so that we can easily exported the data in required format.

Note : In the above example i have used the xp_cmdshell stored procedure. This feature required to be enable by using SQL Server Surface area configuration or you can use the following script to enable the feature.

EXECUTE sp_configure 'show advanced options', 1
EXECUTE sp_configure 'xp_cmdshell', '1'


Total article views: 4733 | Views in the last 30 days: 2
Related Articles

URGENT: Error executing scripts......

URGENT: Error executing scripts......


SSIS error while executing in command line

SSIS error while executing in command line


SQL Script to find the last executed commands on the SQl Server

Sometimes we needs to find out the last executed commands on the database server.  Below is the SQL...


DBCC command execution History

When did a DBCC command executed in a DB


Replication issue Error executing a batch of commands. Retrying individual commands.

My Tran. Replication is working on other server, but when I crete new Subscription on new server I a...