Join Excel spreadsheet to a SQL database - sql

I often get requests that look something like this:
"Hey, can you reconcile this Excel spreadsheet with our database and
fill in the additional info for the ones you find? We also need to know
which ones are missing."
ID Name Client AdditionalInfo
------------------------------------------
234 John Client1
147 Mike Client2
546 Mary Client3
...
(Several hundred or thousand more rows)
Is there some way to take an Excel spreadsheet like that and join it to a SQL database within SQL Server Management Studio? I usually copy the ID to make a massive IN list, which is obviously far from ideal. Then I still have the task of lining up the data if I don't get a 1:1 match for every row.
I'm using SQL Server 2005 and Excel 2010.
I've tried to add an Excel spreadsheet as a linked server but querying it warned me that 'Ad Hoc Distributed Queries' are blocked and must be enabled. I'm not sure what negative implications enabling that may have, so I didn't go any further down that route.
My next thought was to use Excel formulas to make a SELECT [Col_A], [Col_B], [Col_C] UNION ALL for every single row in Excel, then copy that to SSMS to make a temp table I could join... but that still seems like a fairly messy and inefficient hack

You could import your Excel spreadsheet into a table in your database created specifically for that reason. In SSMS, right-click your database, go to Tasks->Import Data and choose Excel as your Data Source.

Excel can connect to Sql Server directly. Specifically, you want to use MS Query. This will allow you to join your Excel worksheet directly against a database using SQL.

Related

Join (INNER JOIN) local Excel table in Oracle SQL Query - VBA

I'm having trouble performing a query on a remote Oracle SQL Server via Excel VBA while trying to perform an INNER JOIN in the same query with a local table in a Excel file sheet.
Example:
Excel Sheet with local table ["LTE_Cells$LTE_Cells_Tmp"]:
Sheet "LTE_Cells"
Oracle SQL Query
SELECT a.STARTDATE, a.ENODEB, a.EUTRANCELLFDD, (a.COUNTER_1/8/1024)+
a.COUNTER2/8/1024) AS Total_Total_Traffic_TB FROM »»»LOCAL_EXCEL_TABLE««««
INNER JOIN REMOTE_DATABASE.LTE_KPI_1 a ON
((»»»LOCAL_EXCEL_TABLE««««.EUTRANCELLFDD =
REMOTE_DATABASE.LTE_KPI_1.EUTRANCELLFDD) AND
(»»»LOCAL_EXCEL_TABLE««««.ENODEB = REMOTE_DATABASE.LTE_KPI_1.ENODEB)) WHERE
(((REMOTE_DATABASE.LTE_KPI_1.STARTDATE)>=sysdate-3));`
Thanks in advance for the help!
This doesn't answer your question directly, and I may be all wet on this but I don't know that either Excel or Oracle explicitly handles what you are trying to accomplish.
However, MS Access will out of the box. Short answer: I think you are using the wrong tool for this task. You are using the proverbial hammer to saw a board in half. Link the spreadsheet and the Oracle table as linked objects in Excel, and your query should be easy-peazy.
Longer answer: while Access does this simply and easily, it can and probably will leave a path of destruction behind it on the DBMS. Specifically, you can expect to thrash the shared pool in Oracle, as Access will be issuing one query (using literals, no less) for every line in Excel. For 1,000 lines, it probably doesn't matter that much, but if you're going to do this on really large datasets, you will make a fast enemy out of your DBA.
Extended answer: really, the best way to do this is to load the contents of those Excel spreadsheets in Oracle tables and let the DBMS do the heavy lifting. This is bread and butter for the RDBMS.

How to left join Excel table to a table in SQL Server database

I have a table (tbl1) in an Excel file with about 70k rows. I have linked that table into Power Pivot. There is another table (tbl2) in SQL Server with millions of rows that I need to left join to the table in my Excel file on
tbl1.[Member Number] = tbl2.[memid]
What query should I use to do it without having to import the whole tbl2 from SQL Server (throws error on Power Pivot due to memory constraints)?
Preferred environment is Power Pivot, but I do have SQL Sever Management Studio. I don't have WRITE permission in the server where tbl2 is located. I do however have WRITE access in a different server.
Thank you!
Import the Excel file into the SQL server where you have WRITE access, do the join there and import the data from this server. Any problem you see with this approach?
A few options spring to mind:
Get more memory/64 bit Excel and use PowerPivot as you are currently trying to do. If you are working on 32bit excel then you are effectively constrained to using 1gb of RAM whereas on 64bit you can use everything you've got. 64bit Powerpivot can potentially deal with hundreds of millions of records.
Read the SQL data and your csv into R, do the join and either write the output to your WRITE db or save as CSV to feed into PowerPivot. Although R has a steep initial learning curve, doing this kind of thing with the dplyr library is straightforward.
Dump your SQL table to csv and read both that your current csv into the WRITE db and do the join in your PowerPivot SQL query.
Which works best probably depends on the skills you have and how often you are doing this. I'd probably go down the 'R' route as you can set it up as a scheduled job.

SQL Server 2012 to Excel 2010 Copy and Paste

I am trying to copy and paste from SQL Server 2012 to Excel 2010.
My query has the 13th column that is delimiting that is ruining the results.
It has a long description field that gets cut off after a space or a certain amount of characters.
It doesn't matter how big or small my order might be or how dumb my questions are, I get great service quickly. My account manager constantly comes through for me, and is the reason all of my purchasing inquiries start with blah
Please help.
I have tried the following methods
Tried saving without any delimiting to the Excel FIle
Changing the Query Results to add Quote Strings contianing list separators when saving .csv or copying result.
Your problem is that you have already used TextToColumn in this instance of Excel. Close Excel. Restart Excel. Execute the query in (I assume you mean) Sql Server Management Studio (AKA SSMS). Left-Click the square in the top left of the results to select all the rows in the query results windows. Then Right-click and select Copy With Headers. Then go back to Excel, select cell A1 and paste. The headers should take care of the column splitting.
Hth,
Ojf
Check if you are able to Export data using DTS Import Export Wizard of SQL SERVER 2012.
Try saving in both Excel(.xls) and Comma Separted file(.csv).

Update sql table from excel

I need users to insert excel data into a sql table on a continuous basis.
They are not sql users, they are barely excel users.
The sprreatsheet is simple, 5 columns, less than 500 rows at a time and all into a single sql table.
In a perfect world they open and update excel and in one or two clicks get a message that says Success.
I am open to any approach.
I have used the import wizard with success, i can write vba code and some sql.
What I don't know how to do is to get a user to inseert excel into a sql table without risking the entire database to complete distruction.
stay well,
michael
you should build a small interface program that the users would update. You can then control it all you want, and update the database as desired, based on their input. Add a single "SAVE" button to save their input.

Extra Long Where/In Statement - Better option?

I've got about 13,000 AccountIDs that I need to pull various data from several data tables using left-joins. The total # of accountIDs is in the millions. I don't have write-access to the server but I was wondering if there was a way I could maybe create a custom/temporary table anyway and do a join to that rather than writing a really, really long Where AccountID in (.....) statement. The accountIDs are currently in a single Excel column so I'd have to get them back in the server somehow.
Thoughts?
You can use OPENDATASOURCE to access your Excel file, but someone will have to push you the Excel file to the server (not ideal).
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\DataFolder\Documents\TestExcel.xls;Extended Properties=EXCEL 5.0')...[Sheet1$] ;
You can ask your DBA team to install a SQl Server Client on your local machine and set up a linked server to the live server, and you are all set. Ask them to set up a view of the data you need, and give you access to the view. That's all you need, I guess.