Export data from SQL Server query to excel - sql-server

I want to export data from a query to excel file. I know that there is a lot of questions like these one here, but no one is acceptable in my situation.
For example, like this topic using OPENROWSET: T-SQL: Export to new Excel file
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;',
'SELECT * FROM [SheetName$]') select * from SQLServerTable
It only execute successfully when I create testing.xls myself, and also define exactly number of columns that will be export from my query in this excel file. Otherwise, an error occur: Column name or number of supplied values does not match table definition.
I also try another solution here: http://weblogs.sqlteam.com/mladenp/archive/2006/07/25/10771.aspx using Exec master..xp_cmdshell
It is really work, but, I heard that xp_cmdshell is a big security threat for SQL Server. So, may be I shouldn't use it.
Is there any other way I can try?

If you're looking for a quick and dirty solution, you can use Management Studio itself.
Here are the steps:
write your query and run it
right click the Results pane and select "Save Results As..."
select your folder/filename and ensure CSV type is selected below
now open your CSV file using Excel; if appropriate save it in Excel as a native Excel format
That won't help you if you need programmatic solution, in which case you have to use Microsoft (Microsoft.Office.Interop.Excel) or 3rd party solutions; you can even build one using Office Open XML.
If you need TSQL solution, OPENROWSET which you are mentioning in the question should be fine.
You can use Excel-only solution and import data using Data pane in the Excel itself (Import from SQL server).
Another possibility taht doesn't have to be coupled to SQL Server or Excel itself is Powershell. However, I'm not into Powershell so if you prefer this method you will have to investigate a bit more.
Also, this is a bit outdated but interesting read.

Try using a newer version of Excel for this. From your query it looks like you’re using Excel 97 – there are probably a ton of bugs and incompatibilities that exist in this version.
If this doesn’t help other options are:
- Do this manually from SSSM like Ozren suggested
- Try creating SSIS package for this
Here are couple other threads to get you started
http://www.connectionstrings.com/excel/
How do you transfer or export SQL Server 2005 data to Excel
Export SQL query data to Excel

Related

How can I pull multiple XML files as text into Excel or SQL Server?

I have dozens of XML files. I would like to store each one as an entry in an SQL Server table of type XML, so that I could query it and manipulate its field data using the built-in functionality the latest version of SQL Server provides. What is an efficient way to do this? I could import each file into a cell in Excel, then import that into SQL Server, but haven't figured out how to do that either, without copy-pasting by hand.
One way is SSIS; another way would be a to write a simple console app. Whichever you're more comfortable coding would probably be the most efficient way for you.

Steps to export the SQL Server returned result set to an Excel file

I have a stored procedure that I am going to run every weekend, it produces a result set that I need to export into an Excel file.
For the above problem I want to automate this process, so I am going to create a SQL Job and I am going to run this stored procedure every weekend so that that generated Excel file is sent to my reporter.
For this I need steps to export the result set data to an Excel file.
And also is it possible to send that Excel file to the specific mail while running the job itself?
So, you might try your luck on https://dba.stackexchange.com/, but in my experience a SQL Agent job running a stored procedure could be coaxed to return CSV or XML - and those could end up in Excel, but there are missing links. I think the missing links would involve programming and potentially 3rd party tools to avoid using Excel's COM API.
I'd strongly recommend your pursuing SQL Server Reporting Services. It is included free with your edition of SQL and includes the ability to
run reports on a schedule (subscriptions),
format the results as an Excel file
distribute the results via email
You'd take your query and use it as the data source for a "report" and use the report wizard to create a very simple table with the results.
Avoid page headers (or footers) that span columns - this will keep the excel output cleaner.
References
Stack Overflow: reporting-services-export-to-excel-with-multiple-worksheets
Technet: Reporting Services

Integrate Excel with SQL Server

I am quite new to SQL Server but I'm looking for a tool that integrates Excel with SQL Server and provide a two way connection Read/write.
I want to be able to pull data from SQL server and perform some evaluation/data manipulation and then write the data back to the server.
Basically my client receives Excel raw data from vendors which they perform some validation on the spreadsheet then send the spreadsheet back but a copy of the data needs to be in some sort of data management system. I have test MDS and I'm not full satisfied. The functionality I'm looking for is
Data validation
Data match - match and merge /consolidate two or more worksheets into one
read/write to sql
I Do not want the import/export wizard and don't want to use SSIS and they are both not suitable.
There's Google - like it has never been before. And there are quite a big number of subject experts posting on their blogs for the love of helping people like you and me.
So check out here multiple ways you can import data into SQL Server, without using SSIS such as,
bcp Utility
e.g.
bcp dbo.ImportTest in 'C:\ImportData.txt' -T -SserverName\instanceName
Bult Insert using T-SQL
e.g.
`BULK INSERT dbo.ImportTest`
`FROM 'C:\ImportData.txt'`
`WITH ( FIELDTERMINATOR =',', FIRSTROW = 2 )`
Note the article was published and last updated in 2012. So you may further check the compatibility for older versions if you are using any.
PS: I still believe you could be using SSIS as not to Re-invent the wheel...

running about 75000 insert statements

What is the best method to import data from an Excel worksheet? As of now I am use SSMS Express so I don't have access to SQL Import Wizard. I also don't have permissions to execute the BULK INSERT command.
My current workflow is as follow: Clean up the excel file, save as CSV, and import it into a SQLite database. Use an IDE like RazorSQL to generate SQL INSERT statements.
This worked nicely until I hit an Excel file about 75000 rows. SSMS just gives an error saying "query finished with errors" or something like that. No error message is shown. I tried adding GO at the end of each line but I got out of memory error.
What are my options?
To answer your question, the best method to import data from excel, in my past experience has been to read excel data into c#, do any clean up and formatting as necessary since excel likes to mess with the data, then use SqlBulkCopy (you only need select/insert permissions) to insert into SQL Server. See this SO answer if you need help reading excel from C#
Update: Given you're not a dev, try using the bcp utility (you should only need select/insert permission)you may need to save the excel file as CSV first, then import it directly into sql server, see this SO answer
You can use following:
bcp utility (between file system data dump and database),
OPENQUERY (can be used from SSMS, works between external datasource like Excel/csv and database),
BULK INSERT (can be used from SSMS, works between external file with user-defined structure and database),
SSIS (usually as dtsx package, has its own GUI, works with various souces and destinations)
Set of INSERT statements (all of them one after another, eventually sliced with GO or packed with UNION ALL)
Set of records serialized in XML variable (can be used from SSMS only; you have to serialize/deserialize it by your self using FOR XML and XML functions)
There are surely other possibilities, but these are maybe most used ones.
EDIT: It seems to me that you could try with GO after every 5-10K lines in your script.
If that doesn't work, XML serialization/deserialization could be the way to go.
Could you use a linked server to connect to the Excel Document? How to use Excel with SQL Server linked servers and distributed queries
A quick and dirty workaround: pull the rows in batches of 50k.
select * from employee limit 50000
select * from employee limit 50000, 100000
From
http://www.razorsql.com/articles/mysql_limit_query.html

how can i get data from excel via "select statement" in sql 2005?

is there any method to get data from excel to sql ? i think that we can do without any writing C#codes. For example : select * from MyExcellFile.xls.Sheet1. Or may be any wizard in sql?
See if this page with example of using OPENDATASOURCE helps.
EDIT: Towards the bottom of the page, you will see an example of a query that uses excel as its source.
You can save your EXCEL file as CSV file, and then you can use the following site in order to TRANSFORM the CSV file into a succession of INSERT statements:
http://csv2sql.evandavey.com/
(FREE Online CSV to SQL Converter)
Please be careful to avoid submitting sensitive data to the site, for obvious privacy reasons.
Easiest way is via the SSIS/DTS wizard. Right click on the database in SQL Server, choose Tasks then Import Data. One of the source data options is an Excel spreadsheet. You can them import it into it's own table in SQL Server or map the columns into existing tables. This is the easiest way for a one time upload.

Resources