SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help needed with BCP command


Help needed with BCP command

Author
Message
wak_no1
wak_no1
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 353
Hello

I'm attempting to use the BCP command for the first time, I'm effectively trying to get the output of a SP copied to a text file, my syntax is as follows:


declare @sql varchar(8000)

SELECT @sql = 'bcp "exec [dbo].[sp_help_revlogin]" queryout "\\servername\d$\directoryname\filename.sql" -c -t -T -S servername'
EXEC xp_cmdshell @sql




The syntax runs without any errors and completes successfully but when I check the location where the output of the query should write to, I find nothing! It's really frustrating when no errors messages are printed, to help me diagnose the issue. Permissions are correct, as far as I can see.

Have I got something wrong in my syntax?
Jeff Atherton
Jeff Atherton
SSC Eights!
SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)

Group: General Forum Members
Points: 975 Visits: 442
Have you tried running the bcp command by itself from a command prompt? That should give you some error messages if it doesn't work.
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4454 Visits: 3672
You need to alter the text "servername" in the BCP command to the name of your server where you want to execute the query. Also alter the text "\\servername\d$\directoryname\" into a valid and existing UNC path.

And as Jeff allready suggested: run the command from a command prompt to see if it works correctly.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
wak_no1
wak_no1
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 353
Jeff Atherton (10/2/2013)
Have you tried running the bcp command by itself from a command prompt? That should give you some error messages if it doesn't work.


Yes, it runs in command. The output just contains a list of switches and their full names (-T Trusted connection, -i Inputfil etc.)
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4454 Visits: 3672
Execute this command from SSMS:
exec xp_cmdshell N'whoami'


Make sure the account listed in the results has the correct permissions to execute the BCP executable and to write to the destination folder. Maybe you should also enter the full path to bcp.exe in the commandline.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
wak_no1
wak_no1
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 353
HanShi (10/2/2013)
Execute this command from SSMS:
exec xp_cmdshell N'whoami'


Make sure the account listed in the results has the correct permissions to execute the BCP executable and to write to the destination folder. Maybe you should also enter the full path to bcp.exe in the commandline.


Yes, the permissions are correct as far as I can see to run the BCP.

Sorry, perhaps I wasn't clear in my original post, I've put 'servername' etc. in the syntax shown to protect my servers name. The actual syntax contains all the correct variables for my server.
tripleAxe
tripleAxe
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1320 Visits: 13295
The stored procedure sp_help_revlogin does not return a result set so it will not work with BCP. You will get an error message like "BCP host-files must contain at least one column" from BCP. The sp_help_revlogin uses the PRINT statement to output the T-SQL for creating logins.

You could use OSQL like in the example below.

declare @sql varchar(8000)

SELECT @sql = 'osql -E -S servername -q"exec [dbo].[sp_help_revlogin]" -o \\servername\f$\outputfilename.sql -w120 -n'
EXEC xp_cmdshell @sql
wak_no1
wak_no1
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 353
Thank you for the reply, I will look into this.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search