Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

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
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'select'.
Did you put double quotes or 2 single quotes?
Avatar of JElster

ASKER

double quote
If I use 2 single I still get Null
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...
Avatar of JElster

ASKER

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
Again it must be queryout and not but
And the records are not displayed, but saved to the file only
Avatar of JElster

ASKER

Still null, no file created. Even tried on server
??????????????????
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
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
Avatar of JElster

ASKER

Same result
NULL

??????????????????????????????????????????
Avatar of JElster

ASKER

No errors just
NULL
Please combine Scott's comment and mine.
Avatar of JElster

ASKER

Tried -T and -U -P  - no luck
Don't see BCP running
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

That worked.
thx