Solved

Export SQL query in CSV

Posted on 2013-10-24
3
555 Views
Last Modified: 2013-10-25
Hi,
I have SQL query which create report for health check purpose. I need to export the same into CSV so that we can  do auto mail of report.

Please post with any sample code for the same.
0
Comment
Question by:kbjijo
  • 2
3 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39598339
Like this using SQLCMD:

SQLCMD -S . -d AdventureWorks2012 -Q “SELECT TOP 10 sp.BusinessEntityID, sp.TerritoryID, sp.SalesQuota, sp.Bonus, sp.CommissionPct FROM Sales.SalesPerson sp” -s “,” -o “e:\result.csv”


or you can run it via t-sql if you have cmdshell enabled like:

exec master..xp_cmdshell N'SQLCMD -S -d AdventureWorks2012 -Q ''SELECT TOP 10 sp.BusinessEntityID, sp.TerritoryID, sp.SalesQuota, sp.Bonus, sp.CommissionPct FROM Sales.SalesPerson sp'' -s '','' -o ''e:\result.csv'''
0
 

Author Comment

by:kbjijo
ID: 39598458
i am using basic query. How can i re direct to CSV

select * DB_Name

Thanks in Advance
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 39598561
First you must check and make sure xp_cmdshell is enabled however...you must check your security policies to see if you can do that - like:


exec sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
exec sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO


Then run a SQL query like:

exec master..xp_cmdshell N'sqlcmd -E -Q"set nocount on; select * from DB_Name.dbo.TableName;" -s"," -W -o"c:\output.csv"'


and please keep in mind the c:\output.csv will be ON the server where SQL is installed.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Row insertion failed. Array 5 45
SSAS Store Forecasting data in the cube 1 17
SQL Server How-To Show Notes In First Row of Results 4 30
job schedule 8 16
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Moving your enterprise fax infrastructure from in-house fax machines and servers to the cloud makes sense — from both an efficiency and productivity standpoint. But does migrating to a cloud fax solution mean you will no longer be able to send or re…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question