Solved

Export a text file

Posted on 2006-06-28
7
1,255 Views
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


output:

usage: bcp {dbtable | query} {in | out | queryout |
format} datafile
[-m maxerrors] [-f formatfile] [-e
errfile]
[-F firstrow] [-L lastrow] [-b
batchsize]
[-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
packetsize]
[-S server name] [-U username] [-P
password]
[-T trusted connection] [-v version] [-R
regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"]
NULL
0
Comment
Question by:earngreen
  • 2
  • 2
  • 2
  • +1
7 Comments
 

Author Comment

by:earngreen
ID: 17001205
Nor does it export the text file.
0
 
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 "'
0
 
LVL 28

Expert Comment

by:rafrancisco
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'
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:earngreen
ID: 17001266
rafrancisco

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

Accepted Solution

by:
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
0
 
LVL 28

Expert Comment

by:rafrancisco
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.
0
 
LVL 21

Expert Comment

by:mastoo
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.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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