Export a text file

Posted on 2006-06-28
Last Modified: 2010-05-18
When I run this in query analyzer I get the out put below. Does anyone know why this occurs?

DECLARE @FileName varchar(50),
        @bcpCommand varchar(2000)

SET @FileName = REPLACE('c:\Test'+ CONVERT(char(8),GETDATE(),112)+'.dat','/','-')

SET @bcpCommand = 'bcp "SELECT * FROM mytable" queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -U username -P pw -password'

EXEC master..xp_cmdshell @bcpCommand


usage: bcp {dbtable | query} {in | out | queryout |
format} datafile
[-m maxerrors] [-f formatfile] [-e
[-F firstrow] [-L lastrow] [-b
[-n native type] [-c character type] [-w
wide character type]
[-N keep non-text native] [-V file format version] [-q
quoted identifier]
[-C code page specifier] [-t field terminator] [-r
row terminator]
[-i inputfile] [-o outfile] [-a
[-S server name] [-U username] [-P
[-T trusted connection] [-v version] [-R
regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"]
Question by:earngreen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1

Author Comment

ID: 17001205
Nor does it export the text file.
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17001212
you have to specify the Servername and the dbname

SET @bcpCommand = 'bcp "SELECT * FROM database.dbo.mytable" queryout "'
LVL 28

Expert Comment

ID: 17001248
There is no -password option for the bcp.  Try removing that and see if it works:

SET @bcpCommand = @bcpCommand + @FileName + '" -U username -P pw'
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.


Author Comment

ID: 17001266

 I just used username and password as an example.
LVL 75

Accepted Solution

Aneesh Retnakaran earned 500 total points
ID: 17001269
Here is an exaaple

BCP Northwind.dbo.Categories out c:\categories.txt -n -Smyservername -Uusername -Ppassword

If you are using Windows authentication
BCP Northwind.dbo.Categories out c:\categories.txt -n -Smyservername  -T
LVL 28

Expert Comment

ID: 17001317
What I was trying to point out is that you specified 2 values for the password like this:

-P pw -password

Just make sure that there is only 1 value for this.
LVL 21

Expert Comment

ID: 17001385
And after you make those two changes, I think you'll find you need to add -n to tell it what format to output.

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question