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

asked on

SQL Server - Export to CSV

Looking for a Stored Procedure that I can pass in a SQL statement and filename and output to CSV comma delimited with first row field names.
any ideas?
Avatar of EvilPostIt
EvilPostIt
Flag of United Kingdom of Great Britain and Northern Ireland image

You can export data via the BCP command and via an SSIS package discounting the CLR way of doing it.

To do it via a stored procedure though is more difficult as you need to run BCP commands using the command line and xp_cmdshell is usually locked down due to security issues.

Do you have any issue with this?
Avatar of JElster

ASKER

Do you have a BCP code sample... ?  I can't find one that works.
thx
SOLUTION
Avatar of Surendra Nath
Surendra Nath
Flag of India 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

I get

'sqlcmd' is not recognized as an internal or external command,
operable program or batch file.
NULL

Any ideas?
Ok, it seems the Path variable on the server, where you are connecting is not set properly... can you let us know the version of sql server you are working with?
Avatar of JElster

ASKER

Using Management Studio 2008. But I believe the SQL server is 2000
if you are using sql 2008, then ensure that your PATH variable is having the below paths included

C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;
C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;

Open in new window

Avatar of JElster

ASKER

Where is the PATH variable set?
Is SQL Server installed on the system you are running the BCP command from?
Avatar of JElster

ASKER

sql is not on running BCP from..
I will try the path thing
Avatar of JElster

ASKER

I get the same message when I run on the sql server box
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

I get all these errors...............?????

SQLState = 08001, NativeError = 17
Error = [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.
SQLState = 01000, NativeError = 53
Warning = [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
You need to change the bit that says servername\instance to your SQL instance.
Avatar of JElster

ASKER

I get the same message.... it works for you?
Yes this works for me. Could you post the block of code that you are using?
Avatar of JElster

ASKER

declare @sql nvarchar(500),@sql1 nvarchar(4000)
declare @FilePath nvarchar(500)
set @sql = 'SET NOCOUNT ON; select top 10 * from cfgoa.test'
set @FilePath = 'C:\myfile.csv'
set @sql1 = 'BCP "'+@sql+'" queryout "'+@FilePath + '" /T /S  CFGSQL2\local /c '
exec master.dbo.xp_cmdshell @sql1
I copied your code, changed the instance and table name and it works fine for me. Could this be a connectivity issue on your server?

I get the following output!

NULL
Starting copy...
NULL
7 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 1      Average : (7000.00 rows per sec.)
NULL
Avatar of JElster

ASKER

I can connect fine to the DB. Any other ideas?
thx
What service account is the SQL Server service running under?