I often find myself having to upload data in Excel files into temp tables in SQL Server in order to do ad hoc updates or comparisons. Users will commonly submit an Excel file containing data to verify, insert, update or what not. I knew there was a way to have SQL Server read data directly form Excel, so finally I got round to trying that out to make these ad hoc tasks easier. Here is how you do it:
First, make sure the data in the XL sheet looks nice and tidy, i.e. that each column has a header in the first row that has a meaningful name for your query. Try to avoid strange characters or lengthy names. For instance, SKU and CasesPerPallet. Clear out breaks in the data or other things that interrupt the flow of numbers, like subtotals, headings etc. The data should all be in a contigous block under the row header. Save the file in Excel 97-2003 workbook format.
Now, copy the file to the file system of the SQL Server (not your own local computer), for instance to D:\Data\CSPallet.xls.
Next, open a query window and try selecting from the file like this:
select
Note the path and filename in the connection string.
You may get an error when running this if you have not turned on access to distributed ad hoc queries on your SQL server. It is off by default. You can do that like this:
sp_configure 'show advanced options',1reconfigure with overridegosp_configure 'Ad Hoc Distributed Queries',1reconfigure with overridego
If you want to join your data to data in the SQL server you can do something like this:
from
join
where
In this case, I am selecting all items from my II_MasterItem table that have a Case Per Pallet count that differs from what was in the Excel sheet.
Hope this helps you run ad hoc joins to XL data more easily!
Remember Me
a@href@title, i, strike, u
Copyright © 2003-2008 Falafel Software Inc.
Subscribe to Falafel Blogs
The opinions expressed herein are Falafel's employees own personal opinions and do not represent Falafel Software's view in any way in case they go bananas!