Excel Import Data via SQL Query - sql

I would need to import the data from SQL Server using SQL query to get the data. I do this via Connection from Microsoft Query. The problem is, the data can be only imported as Table or Pivot Table. I need to import the data which are not formatted as table (due running VBA macro which does not work on data that are in table format).
Is there any option to import the data this way, just pure content?

This is the one I'm using since long
You can give it a try,
select *, ID
from
OPENROWSET('MICROSOFT.ACE.OLEDB.12.0', 'Excel 12.0;HDR=YES;DATABASE=C:\MyData.xlsx', sheet1$) where ID IS NULL

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.

Import sql query on ODBC table in MS Access 2007

Using MS Access 2007 I would like to retrieve only part of an ODBC table.
I can import the whole table in Access but I don't need all of it and it would be a waste of space and performance to store the whole table when I only need certain columns.
In Excel I wrote a SQL query that let me retrieve only the part I'm interested in. What I'd like to know is: is it possible to import only the result of a SQL query in Access or do I have to retrieve the whole table and then run the query on it?
Is it possible using built-in Access module or should I turn to VBA?
Edit: Basically I would like to run the ODBC data connection below (currently used in Excel) in Access.
Connection string:
DSN=BLA1;
UID=BLA2;
DBQ=BLA3;
PWD=BLA4;
DBA=W;
APA=T;
EXC=F;
FEN=T;
QTO=T;
FRC=10;
FDL=10;
LOB=T;
RST=T;
GDE=F;
FRL=F;
BAM=IfAllSuccessful;
MTS=F;
MDI=F;
CSR=F;
FWC=F;
PFC=10;
TLO=0;
Command string:
SELECT *
FROM TEST TEST
WHERE (TEST.DATE_STAMP=?)
When I try to link the database I get the error The database engine can't find 'WTD.DATAPOINT_5/1000'. Make sure it is a valid parameter or alias name, that it doesn't include characters or punctuation, and that the name isn't too long. but when I use the Excel database connection I get no error and everything is updated.
You don't need to import the whole table. You could link to the ODBC table and then run a make-table query against that linked table to copy in only the rows and columns that you need.

Teradata Sql assistant not running SELECT statements

I am trying to run the follwoing sql queries in Teradata sql assistants :
DECLARE #myTablVar TABLE ( tablename varchar(100) )
INSERT INTO #myTablVar VALUES('?')
SELECT * from #myTablVar
Info:
I do not have permission to create tables. So I created the table variable.
I am using Teradata's Importa data feature to insert values into the table variable #myTablVar. There is only one column in the source file.
The system is configured someway such that sql server 2008 queries run only.
Issue:
When I run this, data are imported but the select statement is not
running, and so I can not see the result.
If I put semicoln after each statement then one error occurs :
"Only 1 statement is allowed for this type of import"
Any help?
SQL Assistant Import mode submits the entire query, as a single request, for each record in the Import file.
I'm not sure if SQL Server will like those 3 statements submitted multiple times.
If it is allowed, then the results of the Select statement will be written to a file - not returned to the application display.
Imports never return data to the display - they always write the results to a file whose name is composed of the Import file name plus '.log'. It will be located in the same directory as the Import file.
I don't know a lot about SQL Server 2008 but if it will keep the declared table between queries you could submit the Import first. Then submit the Select after it completes.
Note that prior to version 15.0 Import always submits the entire contents ofthe query window even if you have part of the query highlighted and you have the option set to submit only the highlighted part of a query. (That option did not apply to Import mode until version 15.)

Export data frame to SQL server using RODBC package

I am using RODBC package in R to import / export data frames from SQL Server database. While there is no problem in importing. I dont know how to export the contents of a data frame into an existing SQL table.
I am trying to use sqlQuery() function available in the package, but I am not sure how to insert multiple records in the table.
A sample on how to insert the rows will be helpful
I have ensured that columns of my table and data frame are same.
Use sqlSave with append property. See my code below:
sqlSave(uploaddbconnection, outputframe, tablename =
"your_TableName",rownames=FALSE, append = TRUE)
This is my code on using sqlSave(). I am using SQL Server 2008. Conn is a connection that I created using odbcConnect():
#creating data to be saved in SQL Table
data_to_save<-cbind(scenario_1,scenario_2,scenario_3,scenario_4,store_num,future_date,Province,index)
#use sqlSave() rather than sqlQuery() for saving data into SQL Server
sqlSave(conn,data.frame(data_to_save),"CC_Forecast",safer=FALSE,append=TRUE)
dbWriteTable(conn, "RESULTS", results2000, append = T)
use DBI package
I would like to add upon Yan's answer.
Before you use sqlSave() function, make sure you change your default database to the right database where your table are. Especially if you want to write to an existing table!
See here: for how to set up ODBC connection and how to change default database.
After that, you can use these to dump data to sql:
specificDB= odbcConnect(dsn ='name you set up in ODBC',uid = '***', pwd = '****')
sqlSave(specificDB, output_to_sql, tablename = 'a_table_in_specificDB', rownames = F,append = T)
close(specificDB)
It is slow. be patient.

SQL Import from Excel using non-contiguous range?

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.