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)

SQLCMD to export data in txt file

By Sheraz Mirza,

By using SQLCMD we can export data from SQL Database to a text file in two ways 

  • By using SQLCMD on Command Prompt
  • By selecting SQLCMD Mode in SSMS

By using Command Prompt is as simple as running the command which is mentioned in code section, but please don't forget to replace server name, instance name and database name as per your environment.

By using SQLCMD Mode in SSMS

First you need to select SQLCMD Mode in SSMS by selecting QUERY Menu

then run the code below

Here you can see first 3 rows are highlighted which shows SQLCMD command, actually " : "  tells SSMS that next is SQLCMD command

In first line we are checking if target file already exist then delete it first because in 3rd line of code we are creating target text file.

Second line of code is connecting with SQLServer Instance

Third line is creating Target file

In both ways , one text file will be created on c:drive with column heading and footer detail messages.

Total article views: 787 | Views in the last 30 days: 15
 
Related Articles
FORUM
FORUM

Batch Variable in sqlcmd

Batch Variable in sqlcmd

BLOG

SQLCMD – Explicit and Implicit calls – Examples

The scripting variables can be defined explicitly by using the setvar command or implicitly by using...

FORUM

full cycle structure...command select *

full cycle structure...command select *

BLOG

SQL Server – Add Date/Time to output file of BCP / SQLCMD

You can export data from SQL Server using BCP command for SQLCMD utility. However, these utilities d...

 
Contribute