exporting data from sql server into a CSV using ssms
Solution 1
You could run xp_cmdshell
to run a bcp
operation:
use [master];
declare @sql nvarchar(4000)
select @sql = 'bcp "select * from sys.columns" queryout c:\file.csv -c -t, -T -S'+ @@servername
exec xp_cmdshell @sql
You'd, of course, have to figure out how to format your qualifiers (probably through a format file)
EDIT:
Your source query would need to be something along the lines of:
SELECT IntValue + '"' + CharValue + '"' FROM TABLE
Also, you may need to have this feature enabled
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
GO
Solution 2
You can easily create CSV output from SSMS, however it does not do quoting so you may want to choose a format like Tab delimited instead in step 6:
- Open a new query window.
- Create your SQL query.
- Right click in the query window.
- Choose Query Options...
- Choose Text under Results.
- Change the Output format: to Comma delimited.
- Change the Maximum number of characters displayed in each column to 8000 or an appropriate value.
- Click OK.
- Right click in the query window.
- Choose Results To and Results to File.
- Execute your query.
- Choose a file name and location.
- Click Save.
Solution 3
Creating a text file using a SQL query is not possible. SQL is meant only for fetching,parsring,updating(etc) the data from the database. You need to have sytem executables/dlls to write to a file.
Is there a specific reason why you want to use a SSMS to export the results produced to a csv? Why don't you use SSIS to generate the results for a csv?
If you use SSIS to do this you create a package to export the information and then you schedule the package in a job to run when ever you want it to.
Solution 4
To export into an EXISTING excel file:
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;',
'SELECT * FROM [SheetName$]') select * from SQLServerTable
This is dated, but I believe it is still valid.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926
Cheers!
Alex Gordon
Check out my YouTube channel with videos on Azure development.
Updated on June 07, 2022Comments
-
Alex Gordon almost 2 years
I need to export data from several tables in SQL Server 2008 using SSMS. I do not want to use the native Export Data Wizard; I want to use a query instead. This means I cannot use
sqlcmd
orbcp
.How can I export data out of SQL Server 2008 using a query?
I need it to be comma delimited and double quoted as a text qualifier.
Thanks so much for any guidance/help.
-
Alex Gordon almost 12 yearsthank you - how would i pass parameters into a query this way?
-
Alex Gordon almost 12 yearsi need this automated because i need to pass parameters into the query
-
Alex Gordon almost 12 yearsthank you very much. my i CAN do a sqlcmd without a format file? i dont want to use a format file, is this posible?
-
Saurabh R S almost 12 yearsIf you adopt the SSIS approach, a simple select query will do the task. See this if you wish to learn how to do this using SSIS.
-
Tom almost 12 yearsNo. However, you could use automation to change the xls to csv. Why CSV over xls?
-
Alex Gordon almost 12 yearsxls will not support the amount of rows that i need
-
swasheck almost 12 yearssure, but you'll have to put a lot more work in to put in text qualifiers
-
Alex Gordon almost 12 yearsgreat! can you get me started?
-
swasheck almost 12 yearsIt really just becomes a matter of formatting a query such that it includes
"
where you need it. -
Alex Gordon almost 12 yearsunderstood! i tried to do the cmdshell but getting SQLState = S1000, NativeError = 0 Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file NULL
-
Alex Gordon almost 12 yearsthanks so much , i saw your update however i am getting the same error
-
swasheck almost 12 yearsCould be something along these lines? social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/… or social.msdn.microsoft.com/Forums/en/transactsql/thread/…
-
jcolebrand almost 12 years@АртёмЦарионов it is not considered polite to ask people here to login to your box for a gotomeeting to review your code. We need interaction to occur here on this site, so that people can have answers for their problems. Encouraging someone to come login to your box doesn't help the next person who has this same problem. And if your problem is that unique, maybe you're working on solving the wrong problem?