Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Exporting SQL Query Results

Author  Topic 

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2015-02-20 : 13:02:41
Hello -

Can anyone help me? I have been trying to find a way to export the results of a query to a csv for use in excel but cannot. Can anyone help with this?

Its really frustrating, such an easy idea but so difficult.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-02-20 : 14:21:34
There are a few ways. You can use the import/export wizard: right click on the database, tasks, export data. You'll specify a query for the source.

You can also use bcp.exe, save results as option in SSMS, or OPENROWSET.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

pradeepbliss
Starting Member

28 Posts

Posted - 2015-02-24 : 04:31:12
1) First enable XP_cmdshell in sql server then go to step 2
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO

2)EXEC xp_cmdshell 'bcp "SELECT * FROM DBName.TableName" queryout "E:\Pradeepbliss.csv" -T -c -t "|",'
Go to Top of Page
   

- Advertisement -