Answered by:
SP output to CSV file

Question
-
Gurus,
I have a SP and now i want that output of that SP should be stored in CSV file.
Please Help
Regards
Nitin
nitinThursday, July 30, 2009 5:23 AM
Answers
-
I want to use a sql file as input which will contain the command and the output shud be in .csv
Regards
nitin
You can use bcp to export to csv: Creating CSV Files Using BCP and Stored Procedures
To execute the script, you can use SQLCMD with the -i option (input file containing T-SQL script).
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Zongqing Li Thursday, August 6, 2009 2:41 AM
- Edited by Kalman Toth Wednesday, October 3, 2012 9:05 PM
Thursday, July 30, 2009 4:56 PM -
sqlcmd -S CAFRFD1LITPMA02 -i c:\data\export.sql -o c:\data\text2.txt -s ,
The -i file T-SQL script file must contain all sql script including the bcp logic:
DECLARE @FileName varchar(128),
@BCP varchar(2048)
SET @FileName = REPLACE('c:\data\export\Person'+CONVERT(char(8),GETDATE(),112)+'.txt','/','-')
SET @BCP = 'bcp "SELECT TOP (100) BusinessEntityID, QUOTENAME(FirstName,CHAR(34)), QUOTENAME(LastName,CHAR(34)) FROM AdventureWorks2008.Person.Person ORDER BY LastName" queryout "'
SET @BCP = @BCP + @FileName + '" -c -t, -S "DELLSTAR\SQL2008" -T'
PRINT @BCP
EXEC master.dbo.xp_cmdshell @BCP
The resultant csv file will be what is defined in @FileName for example c:\data\export\Person20090731.txt
bcp "SELECT TOP (100) BusinessEntityID, QUOTENAME(FirstName,CHAR(34)), QUOTENAME(LastName,CHAR(34)) FROM AdventureWorks2008.Person.Person ORDER BY LastName" queryout "c:\data\export\Person20090731.txt" -c -t, -S "DELLSTAR\SQL2008" -T
The c:\data\text2.txt will contain execution messages. You may not need it.
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Kalman Toth Friday, March 2, 2012 5:53 PM
- Edited by Kalman Toth Wednesday, October 3, 2012 9:06 PM
Friday, July 31, 2009 5:27 PM
All replies
-
Hi,
execute that sp in Sql server Managment studio and set option 'output as file'. when you execute procedures it asks to save that file then save it as 'file.csv'.
execute that sp in Sql server Managment studio. Copy data into excel and save file as csv.
Shamas Saeed MCITP-DBD 2005 http://sqlservercoollinks.blogspot.comThursday, July 30, 2009 5:28 AM -
Where do i set this option " 'output as file'" ? Actually then i need to run this in a bacth file and schedule it.so i dont think this option will work!!
nitinThursday, July 30, 2009 5:30 AM -
Hi,You can do this by using OPENROWSET.Please refer: http://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/Let me know if it helps you.Thanks,
Tejas Shah
SQL YOGA- Proposed as answer by Shamas Saeed Thursday, July 30, 2009 5:43 AM
Thursday, July 30, 2009 5:31 AM -
Hi,
You can do this by using OPENROWSET.Please refer: http://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/Let me know if it helps you.Thanks,
Tejas Shah
SQL YOGA
Just don't try it on the 64 bit version because the "Jet" drivers are not supported on the 64 bit version. Nice, huh?
--Jeff ModenThursday, July 30, 2009 6:23 AM -
Hi,yes that is true, I am also can not access it on 64 bit laptop. When we can able to get JET driver for 64bit computer... !!!!Thanks,
Tejas Shah
SQL YOGAThursday, July 30, 2009 6:27 AM -
Heh... get a bat and follow me to Redmond... ;-)
--Jeff ModenThursday, July 30, 2009 7:06 AM -
Gurus
If i have a query,how can use that for csv output, i dont have the permision to create a sp?
Regards
nitinThursday, July 30, 2009 4:00 PM -
Ummmm.... create the SP and have the DBA install it for you?
--Jeff ModenThursday, July 30, 2009 4:22 PM -
I want to use a sql file as input which will contain the command and the output shud be in .csv
Regards
nitinThursday, July 30, 2009 4:44 PM -
I want to use a sql file as input which will contain the command and the output shud be in .csv
Regards
nitin
You can use bcp to export to csv: Creating CSV Files Using BCP and Stored Procedures
To execute the script, you can use SQLCMD with the -i option (input file containing T-SQL script).
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Zongqing Li Thursday, August 6, 2009 2:41 AM
- Edited by Kalman Toth Wednesday, October 3, 2012 9:05 PM
Thursday, July 30, 2009 4:56 PM -
Hey Kalman
Can i use bcp with the input file option?
Regards
nitinThursday, July 30, 2009 4:58 PM -
Thanks kalman, it worked with SQL CMD.
Thanks a ton
Regards
nitinThursday, July 30, 2009 5:28 PM -
Data is not coming in the right format in CSV,can we format the data like we do in BCP ?
Thanks
Nitin
nitinFriday, July 31, 2009 7:02 AM -
sqlcmd -S kanna-clac9d7ec -i c:\text1.sql -o c:\p2.csv
I am using this command and data is not in the right format in csv
Please advice
Regards
nitinFriday, July 31, 2009 7:15 AM -
Friday, July 31, 2009 4:26 PM
-
sqlcmd -S CAFRFD1LITPMA02 -i c:\data\export.sql -o c:\data\text2.txt -s ,
The -i file T-SQL script file must contain all sql script including the bcp logic:
DECLARE @FileName varchar(128),
@BCP varchar(2048)
SET @FileName = REPLACE('c:\data\export\Person'+CONVERT(char(8),GETDATE(),112)+'.txt','/','-')
SET @BCP = 'bcp "SELECT TOP (100) BusinessEntityID, QUOTENAME(FirstName,CHAR(34)), QUOTENAME(LastName,CHAR(34)) FROM AdventureWorks2008.Person.Person ORDER BY LastName" queryout "'
SET @BCP = @BCP + @FileName + '" -c -t, -S "DELLSTAR\SQL2008" -T'
PRINT @BCP
EXEC master.dbo.xp_cmdshell @BCP
The resultant csv file will be what is defined in @FileName for example c:\data\export\Person20090731.txt
bcp "SELECT TOP (100) BusinessEntityID, QUOTENAME(FirstName,CHAR(34)), QUOTENAME(LastName,CHAR(34)) FROM AdventureWorks2008.Person.Person ORDER BY LastName" queryout "c:\data\export\Person20090731.txt" -c -t, -S "DELLSTAR\SQL2008" -T
The c:\data\text2.txt will contain execution messages. You may not need it.
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Kalman Toth Friday, March 2, 2012 5:53 PM
- Edited by Kalman Toth Wednesday, October 3, 2012 9:06 PM
Friday, July 31, 2009 5:27 PM