SQL Import from Excel using non-contiguous range? - sql

I have some Excel spreadsheets that I cannot change as they are used by another department and they will not change them in future. They are .xlsm with over 500 columns (A:TH). I'm trying to import them into SQL server 2008 on a 64bit machine but I'm having huge problems. All forms of Excel import appear to truncate the columns I select to the first 255.
Ultimately there will 5 separate tables to store this data with 1 common key. I could write a short VBA script to sort the data in Excel into arranged columns of tables at source but I wanted to ask if the following was possible first...
This works fine and selects the columns A:IV
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\NEW.xlsm',
'SELECT * FROM [Details Sheet$A:IV]')
Is there a clever way to do something similar with a non-contiguous range such as
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\NEW.xlsm',
'SELECT * FROM [Details Sheet$C:C,IW:LZ]')
ie. pick up the key in column C and the additional columns IW:LZ? The problem for me is that using the full range C:LZ and SELECT [ID],[THIS],[THAT] FROM etc won't work for fields beyond 255 columns in the range, very annoying!

Have you tried using SSIS to import the Excel files? It can be very picky about data types, but I've never run into a limitation that I couldn't work around with a bit of a Script Component.
It's designed to be a high-performance ETL tool for jobs like what you're trying to accomplish. If you're new to it, check out this article on importing the entirety of Wikipedia as XML into multiple tables.
A quick note is that you may need to install additional Office drivers to read Excel 2007 format, especially on 64-bit machine.

Related

Insert Excel Spreadsheet into Oracle Table using SQL Statement

I know there is a way to import an Excel spreadsheet into Oracle using SQL Developer. However, I am trying to find out if it is possible to import an Excel spreadsheet into an Oracle table using a SQL query statement. I have done this type of SQL query previously going from Excel to MS Access but am not trying to do the same thing for going from Excel to Oracle.
The query I have used for going from Excel to Access is as follows:
SELECT * INTO TABLENAME FROM ('Microsoft.Jet.OLEDB.4.0','Excel 12.0;IMEX=1;HDR=NO;DATABASE=EXCELPATH.xlsx', 'Select * from [EXCELSPREADSHEET$]');
There is the way but it isn't easy way.
You can check this Read excel
New excel documents are saved in open xml standard. Thats mean xlsx file is set of zipedd xml files. You can change exension to zip and look what is inside.
You can load seperated xml document into DB as xmltype and use xmlquery to extract data.

Using SQL code with excel power pivot

I'm using SQL code (which I do not know, I just tried to copy from a sample tutorial) to merge documents to use in Excel PowerPivot. I have an excel file called 2014, and another file called 2015. This was the syntax shown in the example to merge the two, but it keeps telling me columns not identified. How can I fix this?
SELECT ['2015$'].* FROM ['2015$']
UNION ALL
SELECT * FROM 'C:\Alex\2014.xlsx'.['2014$'];

How to query data in Excel from Visual Studio 2013?

Background:
I am transferring data from one Excel document doc0 to a templated Excel document doc1 to speed up processes at work. My only real restriction is that I cannot modify the document's formatting, so regular VBA is not an option. I can only pull data out of doc0 modify it and place it in doc1. I am using Visual Studio 2013 for doing so.
What I need to do is:
Organize doc0 numerically by Col 1 first, then Col 3 second. Then place the top 10 results in a specific cell range in doc1.
Get a count for jobs assigned to each worker and return that result to Visual Studio. Worker names are listed in Col 4.
I know how to query using SQL, but am open to using other functions/languages that can perform the same task.
Question:
How can I query the data to perform the actions above?
A simple example can be seen with the link below. The blue represents doc0, the red the results to be displayed in doc1 and the green is the results that I need to have returned to corresponding textboxes in Visual Studio.
There are a few options. ADO.NET is able to connect to your excel sheet using OleDB to read data with simple query capability. Examples can be found in KB316934.
Connect to an Excel file to read and write data:
Connection String
To access an Excel workbook by using the Jet OLE DB Provider, use a connection string that has the following syntax:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties="Excel 8.0;HDR=YES;"
Depending on your excel version, the connection string may slightly differ. Look them up here. E.g. 2013 would look like:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
Read and write data
Use the sheet name followed by a dollar sign (for example, [Sheet1$] or [My Worksheet$]). A workbook table that is referenced in this manner includes the whole used range of the worksheet.
select * from [Sheet1$]
Use a range with a defined name (for example, [MyNamedRange]):
Select * from [MyNamedRange]
Use a range with a specific address (for example, [Sheet1$A1:B10]):
Select * from [Sheet1$A1:B10]
Writing is done in a similar matter if you're using OleDB
INSERT INTO [Sheet1$] (F1, F2) values ('111', 'ABC')
UPDATE [Sheet1$] SET F2 = 'XYZ' WHERE F1 = '111'
You may need to create a temporary copy from which you can query data, as you may be reading and writing to the documents using different techniques.
Full example at the link (unfortunately in VB.NET).
Alternative solutions
If you really want full fidelity access to the Excel file, without depending on Excel being present or running, you could also investigate:
Excel Package Plus
NPIO
Aspose Cells.NET (commercial)
These packages do not support querying, so you'll need to extract the data into objects and use Linq-to-Objects to query/sort the data before writing it back to the files.

Importing Data from .txt file to SQL Server 2005 Express

I have a tab separated .txt (Very Small file with just 10 to 15 datasets) and this file is having some columns as PrdName, PrdSize, PrdWeight, PrdCode and so on.
Now I want to import the two columns which are PrdSize and PrdCode and import it in the columns of my Database table.
I have created the columns but how do I create import clause and transfer data from .txt file to SQL Server? Thanks
Take a look at this post: Import/Export data with SQL Server 2005 Express, there are multiple options that you can use.
Since you have the express edition you'll need to either use BCP or write a program with something else.
If you have a large amount of data, or need to automate the process, definitely look into BCP as mentioned already. However, I often use excel to load one-time data sources (a few hundred to a few thousand) rows of data from odd sources into SQL Server by doing the following:
Get the data into excel (that's usually easy), assuming you get column A with 'Prdsize' and column B with PrdCode, in column C put the formula:
="INSERT INTO MYTABLE(PRDSIZE, PRODCODE) VALUES (" & a1 & "," & B1 & ")"
(in other words create syntactically correct SQL using an Excel formula - you may need to add quotes around string values etc)
and then paste that formula all the way down the column C. Then copy/paste the resultant sql insert statements into SQL Management Studio, or any other tool that can execute SQL and execute it.
Defintely a 'manual' effort, but for one-time data loads it words great.
PS: You'll need to verify the XL formula and the resultant sql syntax - my example is close, but I didn't test it.

Combining data from Excel with database

This is probably a simple question, but I really don't know what I'm doing in Excel, so hopefully someone can help me out.
I've been given an Excel spreadsheet that has two relevant columns to my task. The first column is an "External ID", and the second column is an "Internal ID". I need to select a bunch of data out of our databases (with various joins) using the Internal ID as the key, but then all of this data needs to be linked back to the External ID, and the only link between Internal/External is this spreadsheet.
For example, say a row of the spreadsheet looks like this:
ExtID IntID
AB1234 2
I need to select all the data relevant to the item with ID #2 in our database, but I have no way to get "AB1234" from the database, so I need to somehow relate this data back to "AB1234" using the spreadsheet.
What's the easiest way to accomplish this? The version of Excel is Excel 2007, and the database is Oracle, if that's relevant.
Note that I only have read permission to the production databases, so creating tables and importing the spreadsheet data to do a join is not an option.
Edited based on a comment
1 - Use MS Access to import the Excel sheet as a table.
2 - Link to your database table, also from within MS Access
External Data tab->other data sources->ODBC connection->choose yours->pick the table(s) you want
3 - Write an Access query to compare the values you want
Create->Query Design->Drop the tables you want, drag lines between them for relationships, click Run
Usually I use copy-paste and a good column-mode editor with macros to accomplish such tasks. It works fine if you only have a couple of Excel files.
Alot depends on how familiar you are with the tools you have available to you.
DO you have a tool you are familiar with that would make it easy to use the IntID to find those records? If so, can you do the query and paste the results back into the original spreadsheet in the column to the right of the column with the IntID?
If so, you will have what you want, a spreadsheet with the following columns:
ExtID (original)
IntID (original)
IntID (from Oracle)
Col1 (from Oracle)
Col2 (from Oracle) etc....
I'm not familiar with Oracle, but I know a lot of databases let you prepend a table name with # or something like that and create a temp table. Others have a temporary database where you can create things. Sometimes you can create a temp table even if you can't do anything else but select.
If you have access to do that, I would do the function as JosephStyons suggests (#2), insert your records into the temp table, and do a query based on that.
With Excel and VBA, you can use ActiveX Data Objects (ADO) as a high level way of using the OLE DB provider for a particular database. This lets you read the data from the database and you can then query that data and store the results in the spreadsheet.
Oracle OLE DB provider
ADO Guide