Is there any way to reference a table on an Excel worksheet using OPENROWSET?
Referencing a whole sheet works:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel
12.0;Database=D:\Temp\test.xlsm;IMEX=1;HDR=Yes','select * from [Sheet1$]')
Referencing a range on sheet works too:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel
12.0;Database=D:\Temp\test.xlsm;IMEX=1;HDR=No','select * from [Sheet1$A1:Z100]')
This is not working... trying to reference Table1
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel
12.0;Database=D:\Temp\test.xlsm;IMEX=1;HDR=No','select * from [Sheet1$Table1]')
NOTE:
If this is not possible, then can anybody suggest a different method, maybe using C#/VB.Net to achieve this?
If the range named Table1 was created with the name box it automatically has global scope. If this is the case just query it without referencing the sheet:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;
Database=D:\Temp\test.xlsm;IMEX=1;HDR=No','select * from [Table1]')
If you need Table1 to have local (sheet) scope then it should be named SheetName!Table1, where SheetName should be the sheet intended for Table1.
Some more info about opwenrowset and Excel and Named Ranges in Excel.
Related
I have written a openrowset code which gets table content and saves them in a excel sheet I want to format the data in excel sheet with borders while loading any help is highly appreciated.
insert into OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=M:\abc.xlsx;;HDR=YES',
'SELECT * FROM [Sheet1$]')
select * from [abc]
I've worked on SQL Server 2014 and I need to import data in it from Excel.
'Till now, I've imported data by right-clicking on the current database -> Tasks -> Import Data.
I want to know if there is a SQL syntax to import data directly into a table?
You can try OPENROWSET
Insert into SQLServerTable
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [SheetName$]')
I have an excel spreadsheet with two columns; job_number and referred_by. I have a SQL Server table with the same two columns. Most of the Job Numbers from the excel spreadsheet exist in the SQL server but not all of them.
How would I import all the referred_by into the SQL Server where a matching job_number exists?
If you need to query an excel spreadsheet I would set it up as a linked server, so:
EXEC sp_addLinkedServer
#server= 'xlsx_NewSheet',
#srvproduct = 'ACE 12.0',
#provider = 'Microsoft.ACE.OLEDB.12.0',
#datasrc = 'C:\spreadsheet.xlsx',
#provstr = 'Excel 12.0; HDR=Yes'
With that set up you can almost query it like just another table:
SELECT * FROM OPENQUERY (XLSX_NewSheet, 'Select * from [Sheet1$]')
or
SELECT * FROM XLSX_NewSheet...[Sheet1$]
There are some limitations that you may need to explore. I would suggest just dumping the entire contents into a temp table and operating from there.
I am trying to read data from an Excel using the Following statement which is working absolutely fine
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select MyColum from [Sheet1$]')
the XLS inpout.xls has two columns ID and NAME. Now I want the above statement to return only ONE value/row where ID = x and assign the returned value to a local variable as shown below -
DECLARE #dbValue varchar(20)
DECLARE #I int
SET #I = 1
Select #dbValue = (Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select MyColum from [Sheet1$] where [ID]=#I'))
this returns an error
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "No value given for one or more required parameters.".
Msg 7320, Level 16, State 2, Line 3
Cannot execute the query "select MyColum from [Sheet1$] where [ID]=#I" against OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".
Please suggest.
Maybe something like this will work. But your question is confusing, since your query on the Excel file selects MyColumn, but you say that file has columns ID and NAME.
DECLARE #dbValue varchar(20)
DECLARE #I int
SET #I = 1
Select #dbValue = NAME FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select ID from [Sheet1$]') as T
WHERE ID=#I;
I have an Excel sheet now I need to insert data from that sheet into SQL Server 2005.
I need to create a temp table and insert data into that table. The database name is Employee
Can you pls provide me the syntax for achieving it.
A simple search: http://support.microsoft.com/kb/321686
Probably easiest is
SELECT *
INTO #tmptable
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [SheetName$])
--Excel 2007-2010
INSERT INTO DATABASE.SCHEMA.TABLENAME
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\SheetName.xlsx','SELECT * FROM [SheetName$]');
--Excel 97-2003
INSERT INTO DATABASE.SCHEMA.TABLENAME
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.4.0',
'Excel 8.0;HDR=YES;Database=C:\SheetName.xls','SELECT * FROM [SheetName$]');