views:

40

answers:

2

I am trying to export data into into a text file using bcp utility? I could n't figure out what i am doing wrong here:-

Declare @Cmd nvarchar(1000)
Declare @Query nvarchar(1000)
Declare @Path nvarchar(500)
Declare @ServerName nvarchar(500)
Declare @UserName nvarchar(50)
Declare @Password nvarchar(50)
Declare @Delimiter char(1)

SET @Path = 'c:\abc.txt'
SET @ServerName = '10.10.10.10\instance1'
Set @UserName = 'sa'
Set @Password = '123456'
Set @Delimiter = '!'

SET @Query = 'SELECT [ID]
          , NULL
          ,[NAME]
          ,[STREET1] + '' '' + [STREET2] as [Address]
          ,[CITY] + '' '' + [PROVINCE] + '' '' + [POSTAL] as City
          ,[PHONE] 
          ,[FAX]
          ,''Title'' as [Title]
          ,[Country]
FROM [temp01]'
SET @Cmd = 'bcp "' + @Query + '" queryout "' +  @Path +
         '" -c -S' + @ServerName + ' -U' + @UserName + ' -P' + 
        @Password + ' -t' + @Delimiter + ''
EXEC master..xp_cmdshell @Cmd

It is not writing any data into the file. In result box i am getting this 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 [-h "load hints"] NULL

Please help.

+2  A: 

I think you need a space after your -S switch. Also, in general, when debugging something complex reduce the complexity until you can identify where the problem is. In this case, you can remove each switch one at a time (or remove all of them to start) to determine whether or not that's causing the problem.

Beth
@Beth: No, unfortunately its not a space after -S Switch?
Novice
try changing your code to SET @Cmd = 'bcp "' + @Query + '" queryout "' + @Path just for testing. Also, try simplifying your query like SET @Query = 'SELECT [ID] FROM [temp01]'
Beth
A: 

You do have to give a database. you only defined the instance an a table but no database. try change your select-statemen like : ...from [yourDB].[temp01]...

Ice
Ya... I tried with DB Name but same error..
Novice