SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Export Data in Text File Using SQLCMD in SQL SERVER 2005

By Manish Mathur, 2009/02/27

Total article views: 2521 | Views in the last 30 days: 71

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
RECONFIGURE WITH OVERRIDE
GO
EXECUTE sp_configure 'xp_cmdshell', '1'
RECONFIGURE WITH OVERRIDE
GO

 

By Manish Mathur, 2009/02/27

Total article views: 2521 | Views in the last 30 days: 71
Your response
 
 
Related Articles
FORUM

URGENT: Error executing scripts......

URGENT: Error executing scripts......

FORUM

SSIS error while executing in command line

SSIS error while executing in command line

FORUM

DBCC command execution History

When did a DBCC command executed in a DB

FORUM

script errors

script errors

FORUM

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...

Tags
sqlcmd    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com