[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Help with BCP

The following returns null.

OUTPUT
NULL

Any ideas what I'm doing wrong

declare @sql varchar(8000)
select @sql = ' bcp select LAST_NAME, FIRST_NAME from customers ORDER BY LAST_NAME  out c:\TEMP\1.CSV -T -C -S'+ @@servername

exec master..xp_cmdshell @sql
0
JElster
Asked:
JElster
  • 8
  • 6
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Bcp  out is only for a full table/view
You want queryout and need quotes

declare @sql varchar(8000)select @sql = ' bcp "select LAST_NAME, FIRST_NAME from customers ORDER BY LAST_NAME"  queryout c:\TEMP\1.CSV -T -C -S'+ @@servername
exec master..xp_cmdshell @sql
0
 
JElsterAuthor Commented:
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'select'.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Did you put double quotes or 2 single quotes?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
JElsterAuthor Commented:
double quote
If I use 2 single I still get Null
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
The syntax is correct.
How are you running this code?
When you run this on a client computer, the bcp command is run on the client, not on the server...
0
 
JElsterAuthor Commented:
Running on a client computer using Management Studio  
Here's the actual code, returns null the individual select returns 1000s of records
thx!

print @@servername
declare @sql varchar(8000)
select @sql = ' bcp ''select LAST_NAME, FIRST_NAME, REPNO,  CONFQUAL_INS_INDIV  from vw_CQ_RevenueTotal ORDER BY LAST_NAME''  out c:\TEMP\1.CSV -T -C -S '+ @@servername
exec master..xp_cmdshell @sql
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Again it must be queryout and not but
And the records are not displayed, but saved to the file only
0
 
JElsterAuthor Commented:
Still null, no file created. Even tried on server
??????????????????
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Now I see the issue
Being off a computer (on mobile , on holiday , near the sea ... ) I overlooked the obvious missing part...

Step 1: kill all bcp.exe on the client and the server. They are waiting for username/password entry

Step 2 : http://msdn.microsoft.com/en-us/library/ms162802.aspx
Add either -T (trusted connection )or  -Uusername -Ppassword to the bcp command string
0
 
Scott PletcherSenior DBACommented:
Since you're not specifying the database, you're likely starting in master, which won't contain the view.  Try fully qualifying the view name.

Other adjustments:
Do NOT put a leading space before the program to run.
Bcp parameters are case sensitive -- thus, it must "-c" rather than "-C".
I prefer to space out the parameters to make it easier to read/change, but naturally you can compact it if you don't like it:

select @sql = 'bcp ' +
    '''select LAST_NAME, FIRST_NAME, REPNO,  CONFQUAL_INS_INDIV  from your_db_name.dbo.vw_CQ_RevenueTotal ORDER BY LAST_NAME'' ' +
    'queryout c:\TEMP\1.CSV ' +
    '-T -c ' +
    '-S ' + @@servername
 exec master..xp_cmdshell @sql
0
 
JElsterAuthor Commented:
Same result
NULL

??????????????????????????????????????????
0
 
JElsterAuthor Commented:
No errors just
NULL
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Please combine Scott's comment and mine.
0
 
JElsterAuthor Commented:
Tried -T and -U -P  - no luck
Don't see BCP running
0
 
Scott PletcherSenior DBACommented:
Verify that it's double quotes around the query, not two single quotes.
I also prefer to put double quotes around the file name (in case of embedded spaces, special characters, etc.).

Test it out from SSMS, as that should give you an error message when it doesn't work.

select @sql = 'bcp ' +
     ' "select LAST_NAME, FIRST_NAME, REPNO,  CONFQUAL_INS_INDIV  from your_db_name.dbo.vw_CQ_RevenueTotal ORDER BY LAST_NAME" ' +
     'queryout "c:\TEMP\1.CSV" ' +
     '-T -c ' +
     '-S ' + @@servername
  exec master..xp_cmdshell @sql
0
 
JElsterAuthor Commented:
That worked.
thx
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 8
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now