exporting data from sql server into a CSV using ssms - sql

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 or bcp.
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.

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.

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

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.

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!

You could write a select spiced up with insert.
For instance:
select 'insert into TargetTable values(id, name) values ('''+ id +''','''+ name +''')' from SourceTable

Related

Is there a SQL function that allows me to export the query result to a .csv automatically?

I'm trying to back up all the tables in a SQL Server Database. Here's the logic I'd like to execute:
For Each TableName in MyDatabase
SELECT *
FROM TableName
Export Results to C:\TableName.csv
Loop
*Disclaimer that none of these will ever be a better option than a native backup in terms of backing of data. I'm assuming you simply want some flat file backups.
Several manual options are available from generating the scripts and data via Tasks->Generate Scripts, to manually running a select * from tablename and then right clicking -> Copy including header columns into their own excel files.
More automated options involve SSIS packages to handle this sort of enumerator logic. I believe that the best way to handle this would be a powershell script. Please see #Smor's comment to the following answer: SQL Server Management Studio 2012 - Export all tables of database as csv , for a detailed powershell solution.
The only T-SQL based querying options I'm aware of would be to attempt something of the following:
SELECT
'insert into OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
''Excel 8.0;Database=#filepath\'' + t.name + '.csv '', --#filepath would be replaced with your logic for determining filename+path
''SELECT * FROM [SheetName$]'')
select * from ' + t.name + ';'
FROM
sys.tables
And executing the results of this script.

SQL - Exporting a table with xml colomun into a text file

I need to export a table, that contains a XML column (this xml my contain any special characters, so I can not use them as column delimiters) into a text file.
I am using SQL Server 2014. The XML column may contain special characters like #, |, ,, ?, tab, <cr> many things that could be used as delimiters.
I want to export the whole table. The XML is not structured internally. The max length of the column is around 6000 characters. The table has around 700k rows. And the destination is the same table in a SQL Server 2012 (lower version than the origin), they are in different networks.
I am trying to export it as a .txt file with || as column delimiter. But when I try to import this file into the destination table, it says that the text was truncated and could not be imported.
What's the best way I can do this?
All your answers you gave in your comments give me the idea that you are going the wrong way... The best approach should be linked servers:
Read here: https://msdn.microsoft.com/en-us/library/ff772782.aspx?f=255&MSPPError=-2147217396
Further information here: https://msdn.microsoft.com/en-us/library/ms190479.aspx?f=255&MSPPError=-2147217396
Try this in your SS2014
USE [master]
GO
EXEC master.dbo.sp_addlinkedserver
#server = N'YourLowerServer',
#srvproduct=N'SQL Server' ;
GO
This you need to get access:
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'YourLowerServer',
#locallogin = NULL ,
#useself = N'True' ;
GO
If this is done you can use the INSERT INTO from one server directly to the other server. Try this in your SS2014:
INSERT INTO YourLowerServer.YourDatabase.dbo.TableName(col1,col2,...)
SELECT col1,col2,... FROM dbo.TableName
If you want to get rid of your linked server after this operation use sp_dropserver (read here: https://msdn.microsoft.com/en-us/library/ms174310.aspx)
Hope this helps...

Export a SQL query to XML file

I need to export a SQL query in SQL Server to an XML file.
So far, I have made ​​a query that is:
select *
from
products
for xml path ('product'), root ('Products');
With this query, the result is correct, but I have not found a way to export it to a file.
My idea is to make the export from SQL Management Studio, if possible.
If this option is not possible, I would like to give me a hand to find out what other options I can use.
The other options I've seen are SQLCMD and .NET with Visual Basic.
In Management Studio:
Run query as above.
Click on XML link in results - this will open the XML in a new window.
Navigate to new windown and File -> Save As - this should save as XML by default.
A bit of a manual process but maybe useful for an ad-hoc scenario?
For the sake of others who will be looking for this answer.
This can be achieved in two ways.
use EXEC xp_cmdshell and add queryout "Report.xml" in the syntax for it to save it as a xml file.
EXEC master.dbo.xp_cmdshell 'bcp "SELECT *FROM DataTable" queryout Report.xml -S[ServerName]
use sqlcmd tool. Like this,
just add save your query as input.sql:
sqlcmd -S <your-server> -i input.sql -o Report.xml

How to insert a blob into a database using sql server management studio

How can I easily insert a blob into a varbinary(MAX) field?
As an example:
thing I want to insert is: c:\picture.png
the table is mytable
the column is mypictureblob
the place is recid=1
You can insert into a varbinary(max) field using T-SQL within SQL Server Management Studio and in particular using the OPENROWSET commmand.
For example:
INSERT Production.ProductPhoto
(
ThumbnailPhoto,
ThumbnailPhotoFilePath,
LargePhoto,
LargePhotoFilePath
)
SELECT ThumbnailPhoto.*, null, null, N'tricycle_pink.gif'
FROM OPENROWSET
(BULK 'c:\images\tricycle.jpg', SINGLE_BLOB) ThumbnailPhoto
Take a look at the following documentation for a good example/walkthrough
Working With Large Value Types
Note that the file path in this case is relative to the targeted SQL server and not your client running this command.
MSDN has an article Working With Large Value Types,
which tries to explain how the import parts work, but it can get a bit confusing since it does 2 things simultaneously.
Here I am providing a simplified version, broken into 2 parts. Assume the following simple table:
CREATE TABLE [Thumbnail](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Data] [varbinary](max) NULL
CONSTRAINT [PK_Thumbnail] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ) ON [PRIMARY]
If you run (in SSMS):
SELECT * FROM OPENROWSET (BULK 'C:\Test\TestPic1.jpg', SINGLE_BLOB) AS X
it will show, that the result looks like a table with one column named BulkColumn. That's why you can use it in INSERT like:
INSERT [Thumbnail] ( Data )
SELECT * FROM OPENROWSET (BULK 'C:\Test\TestPic1.jpg', SINGLE_BLOB) AS X
The rest is just fitting it into an insert with more columns, which your table may or may not have. If you name the result of that select FOO then you can use SELECT Foo.BulkColumn and as after that constants for other fields in your table.
The part that can get more tricky is how to export that data back into a file so you can check that it's still OK. If you run it on cmd line:
bcp "select Data from B2B.dbo.Thumbnail where Id=1"
queryout D:\T\TestImage1_out2.dds -T -L 1
It's going to start whining for 4 additional "params" and will give misleading defaults (which will result in a changed file). You can accept the first one, set the 2nd to 0 and then assept 3rd and 4th, or to be explicit:
Enter the file storage type of field Data [varbinary(max)]:
Enter prefix-length of field Data [8]: 0
Enter length of field Data [0]:
Enter field terminator [none]:
Then it will ask:
Do you want to save this format information in a file? [Y/n] y
Host filename [bcp.fmt]: C:\Test\bcp_2.fmt
Next time you have to run it add -f C:\Test\bcp_2.fmt and it will stop whining :-)
Saves a lot of time and grief.
There are two ways to SELECT a BLOB with TSQL:
SELECT * FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a
As well as:
SELECT BulkColumn FROM OPENROWSET (BULK 'C:\Test\Test1.pdf', SINGLE_BLOB) a
Note the correlation name after the FROM clause, which is mandatory.
You can then this to INSERT by doing an INSERT SELECT.
You can also use the second version to do an UPDATE as I described in How To Update A BLOB In SQL SERVER Using TSQL .
However you can simply read a file from disk on SQL server machine:
select * from openrowset (bulk 'c:\path\filename.ext',single_blob) a
to see it in management application in hex form (Management Studio).
So, you can, for example, backup database to file (locally on server) and then download it to other place by the statement above.
Do you need to do it from mgmt studio? Here's how we do it from cmd line:
"C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe" /S < Server> /D < DataBase> /T mytable /C mypictureblob /F "C:\picture.png" /W"where RecId=" /I
Ok... this took me way too long. The sql-management studio tool is just not up to simple things like this (which I've noticed before when looking for where to set the timeout on queries, and it was done in 4 different locations)
I downloaded some other sql editor package (sql maestro in my case). And behold it includes a blob editor where you can look at blobs, and load new blobs into these field.
thanks for the input!

How do you transfer or export SQL Server 2005 data to Excel

I have a simple SQL 'Select' query, and I'd like to dump the results into an Excel file. I'm only able to save as .csv and converting to .xls creates some super ugly output. In any case, as far as I can tell (using Google) this doesn't seem to be so straight forward. Any help would be greatly appreciated.
SSIS is a no-brainer for doing stuff like this and is very straight forward (and this is just the kind of thing it is for).
Right-click the database in SQL Management Studio
Go to Tasks and then Export data, you'll then see an easy to use wizard.
Your database will be the source, you can enter your SQL query
Choose Excel as the target
Run it at end of wizard
If you wanted, you could save the SSIS package as well (there's an option at the end of the wizard) so that you can do it on a schedule or something (and even open and modify to add more functionality if needed).
Use "External data" from Excel. It can use ODBC connection to fetch data from external source: Data/Get External Data/New Database Query
That way, even if the data in the database changes, you can easily refresh.
I've found an easy way to export query results from SQL Server Management Studio 2005 to Excel.
1) Select menu item Query -> Query Options.
2) Set check box in Results -> Grid -> Include column headers when copying or saving the results.
After that, when you Select All and Copy the query results, you can paste them to Excel, and the column headers will be present.
See this
This is by far the best post for exporting to excel from SQL:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926
To quote from user madhivanan,
Apart from using DTS and Export wizard, we can also use this query to export data from SQL Server2000 to Excel
Create an Excel file named testing having the headers same as that of table columns and use these queries
1 Export data to existing EXCEL file from SQL Server table
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;',
'SELECT * FROM [SheetName$]') select * from SQLServerTable
2 Export data from Excel to new SQL Server table
select *
into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')
3 Export data from Excel to existing SQL Server table
Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [SheetName$]')
4 If you dont want to create an EXCEL file in advance and want to export data to it, use
EXEC sp_makewebtask
#outputfile = 'd:\testing.xls',
#query = 'Select * from Database_name..SQLServerTable',
#colheaders =1,
#FixedFont=0,#lastupdated=0,#resultstitle='Testing details'
(Now you can find the file with data in tabular format)
5 To export data to new EXCEL file with heading(column names), create the following procedure
create procedure proc_generate_excel_with_columns
(
#db_name varchar(100),
#table_name varchar(100),
#file_name varchar(100)
)
as
--Generate column names as a recordset
declare #columns varchar(8000), #sql varchar(8000), #data_file varchar(100)
select
#columns=coalesce(#columns+',','')+column_name+' as '+column_name
from
information_schema.columns
where
table_name=#table_name
select #columns=''''''+replace(replace(#columns,' as ',''''' as '),',',',''''')
--Create a dummy file to have actual data
select #data_file=substring(#file_name,1,len(#file_name)-charindex('\',reverse(#file_name)))+'\data_file.xls'
--Generate column names in the passed EXCEL file
set #sql='exec master..xp_cmdshell ''bcp " select * from (select '+#columns+') as t" queryout "'+#file_name+'" -c'''
exec(#sql)
--Generate data in the dummy file
set #sql='exec master..xp_cmdshell ''bcp "select * from '+#db_name+'..'+#table_name+'" queryout "'+#data_file+'" -c'''
exec(#sql)
--Copy dummy file to passed EXCEL file
set #sql= 'exec master..xp_cmdshell ''type '+#data_file+' >> "'+#file_name+'"'''
exec(#sql)
--Delete dummy file
set #sql= 'exec master..xp_cmdshell ''del '+#data_file+''''
exec(#sql)
After creating the procedure, execute it by supplying database name, table name and file path:
EXEC proc_generate_excel_with_columns 'your dbname', 'your table name','your file path'
Its a whomping 29 pages but that is because others show various other ways as well as people asking questions just like this one on how to do it.
Follow that thread entirely and look at the various questions people have asked and how they are solved. I picked up quite a bit of knowledge just skimming it and have used portions of it to get expected results.
To update single cells
A member also there Peter Larson posts the following:
I think one thing is missing here. It is great to be able to Export and Import to Excel files, but how about updating single cells? Or a range of cells?
This is the principle of how you do manage that
update OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=c:\test.xls;hdr=no',
'SELECT * FROM [Sheet1$b7:b7]') set f1 = -99
You can also add formulas to Excel using this:
update OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=c:\test.xls;hdr=no',
'SELECT * FROM [Sheet1$b7:b7]') set f1 = '=a7+c7'
Exporting with column names using T-SQL
Member Mladen Prajdic also has a blog entry on how to do this here
References: www.sqlteam.com (btw this is an excellent blog / forum for anyone looking to get more out of SQL Server).
If you are looking for ad-hoc items rather than something that you would put into SSIS. From within SSMS simply highlight the results grid, copy, then paste into excel, it isn't elegant, but works. Then you can save as native .xls rather than .csv
Here's a video that will show you, step-by-step, how to export data to Excel. It's a great solution for 'one-off' problems where you need to export to Excel:
Ad-Hoc Reporting
It's a LOT easier just to do it from within Excel.!!
Open Excel
Data>Import/Export Data>Import Data
Next to file name click "New Source" Button
On Welcome to the Data Connection Wizard, choose Microsoft SQL Server.
Click Next.
Enter Server Name and Credentials.
From the drop down, choose whichever database holds the table you need.
Select your table then Next.....
Enter a Description if you'd like and click Finish.
When your done and back in Excel, just click "OK"
Easy.
Create the excel data source and insert the values,
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;',
'SELECT * FROM [SheetName$]') select * from SQLServerTable
More informations are available here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926
You could always use ADO to write the results out to the worksheet cells from a recordset object
A handy tool Convert SQL to Excel converts SQL table or SQL query result to Excel file without programming.
Main Features
- Convert/export a SQL Table to Excel file
- Convert/export multiple tables (multiple query results) to multiple Excel worksheets.
- Allow flexible TSQL query which can have multiple SELECT statements or other complex query statements.
B. Regards,
Alex
There exists several tools to export/import from SQL Server to Excel.
Google is your friend :-)
We use DbTransfer (which is one of those which can export a complete Database to an Excel file also) here: http://www.dbtransfer.de/Products/DbTransfer.
We have used the openrowset feature of sql server before, but i was never happy with it, becuase it's not very easy to use and lacks of features and speed...
Try the 'Import and Export Data (32-bit)' tool. Available after installing MS SQL Management Studio Express 2012.
With this tool it's very easy to select a database, a table or to insert your own SQL query and choose a destination (A MS Excel file for example).
you can right click on a grid of results in SQL server, and choose save as CSV. you can then you can import this into Excel.
Excel gives you a import wizard, ensure you select comma delimited. it works fine for me when i needed to import 50k+ records into excel.
Check this.
Query -> Query Options.
Results -> Grid -> Include column headers when copying or saving the results