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
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
That worked.
thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.