Importing Excel Data into SQL Server 2008 R2 Express - sql-server

There seems to have been many discussions on this but I couldn't find something specific to what I am looking for. I am trying to use a query to import data from Excel to an existing table in SQL Server.
My query is:
INSERT INTO DailyRawData
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=NO;
Database=C:\Users\home\Desktop\SQLImportTrim.xls', [data$]);
I get the following error:
Msg 7314, Level 16, State 1, Line 2
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" does not contain the table "data$". The table either does not exist or the current user does not have permissions on that table.
I don't think this is a permission issue for me as I am set up as SysAdmn. I am wondering if the error is due to the last part of the query [data$] since this is what the error msg refers to. FYI the name of the excel file is SQLImportTrim and the tab that contains all my data is named data. There is no table named data in my Excel file. Is my query correct?

you don't use [data$] you use the name of the sheet, so the standard starting point is usually [Sheet1$] if you haven't renamed it.

Related

T-SQL Openquery - error due to space in the table name

I have the below query that is trying to pull data from Sage 50 pervasive 13 database into SQL server using a link server. I've been able to pull all data from all tables into SQL Server except for this one table because it has a space in the table name.
I've not been successful with anything I've changed it to. Can anyone help me get this query working?
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1.Budget Details')
When I change the above query to this:
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1.[Budget Details]')
I get this error message:
OLE DB provider "MSDASQL" for linked server "ARKSAGE" returned message "[PSQL][ODBC Client Interface][LNA][PSQL][SQL Engine]Syntax Error: select * from NEPHROPATHOLOGYASSO1.<< ??? >>[Budget Details]".
Msg 7321, Level 16, State 2, Line 61
An error occurred while preparing the query "select * from NEPHROPATHOLOGYASSO1.[Budget Details]" for execution against OLE DB provider "MSDASQL" for linked server "ARKSAGE".
The PSQL in the error message tells me the linked server is probably running Postgresql, rather than SQL Server. Postgresql marks object names with double quotes instead of brackets. Therefore you should try this:
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1."Budget Details"')
Additionally, I'm not sure what the << ??? >> text is for, but it looks a little like it's complaining about an odd unicode character in there somewhere. So look out for invisible whitespace. Or maybe it's just part of how the error message is formatted in the context of a linked Postgresql server.

SQL Server -> Linked Server to Oracle: "inconsistent metadata" on n+1st query

I use LinkedServer to connect SQL-Server with Oracle.
No matter if I use openquery or not, I can only query one single action like "delete" or "insert" on my LinkedServer-Connection. For the n+1st query, I get the following error from SQL-DB:
The OLE DB provider "OraOLEDB.Oracle" for linked server "NAME"
supplied inconsistent metadata. An extra column was supplied during
execution that was not found at compile time.
When I check sessions on OracleDB, I can see that immediately after my query, there is an inactive session from the SQL-Server-Host with a wait-event "SQL*Net message from client". This inactive session disappears after about 60s. Until then, I cannot query any other actions on my LinkedServer-Connection - unless I kill this session manually.
How can I prevent this session from being created and blocking further queries?
Linked-Server-Config:
Provider: Oracle Provider for OLE DB
Provider-Options: "Level zero only" = TRUE, "Allow inprocess" = TRUE
Linked-Server Options: default
Thanks in advance,
Martin
EDIT: Minimal-Repro
At first, I insert data into empty oracle table via
INSERT OPENQUERY
(LINK, 'select COLUMNS from LINKED_TABLE') VALUES (VALUES);
This works.
Next, I insert more data into oracle table, e.g.
INSERT OPENQUERY
(LINK, 'select COLUMNS from LINKED_TABLE') VALUES (DIFFERENT_VALUES);
Now, I get the following error in SSMS:
Msg 7353, Level 16, State 1, Line 5
The OLE DB provider "OraOLEDB.Oracle" for linked server "LINK" supplied inconsistent metadata. An extra column was supplied during execution that was not found at compile time.
If I check oracle sessions, I can see that there is an inactive connection-session for my SQL-Server-Host. Only if I kill this connection-session in Oracle SQL Developer, I can insert (or delete/update) more data. "Select" works without problems.
Hope this helps.

Find Column Data type in linked server

I just started working on a new project where data is stored in linked server. I have to add new columns to a table in staging environment, which already exists under same table name in linked server.
I have dumped data into a temp table but all the columns with data type decimals, int and numerics are converted into float in staging database. Also, when I'm trying to access information_schema or sys.columns for metadata getting below error. Any workaround to see the data type of table columns in linked server ?
select * from openquery(LINKEDSERVER,'select * from information_schema.columns')
Error:
OLE DB provider "MSDASQL" for linked server "LINKEDSERVER" returned message "[LINKEDSERVER][ODBC 64bit driver][OpenAccess SDK SQL Engine]Base table:columns not found.[10129]".
Msg 7321, Level 16, State 2, Line 45
An error occurred while preparing the query "select * from information_schema.columns" for execution against OLE DB provider "MSDASQL" for linked server "LINKEDSERVER".
Thanks in advance.
If you can query the linked server, then the following would work:
select
*
from [LinkedServerInstanceHere].[DatabaseOnLinkedServerHere].information_schema.columns
Just populate your details in [ ]...
I just tested it and it works on my side.
Hope this helps.

Inserting into a table from view create witha link server

I have created a view that includes a view from a linked server and I am inserting into a table. when I query the table it returns no data when the where statement is referencing data from the linkserver table. It does however pull all the data when not using the where statement. why is this for I do not have much experience working with LinkedServers.
Here is code I used to build my view i am using to populate my table.
ALTER VIEW [dbo].[weightsDashboard]
AS
SELECT *
FROM [dbo].[weightsYak]
UNION ALL
SELECT *
FROM OPENQUERY([10.3.50.62\AGJET], 'SELECT * FROM [CheckWeigher].
[dbo].[weightsSs]')
Then I ran this to create the dataset I want to report off of.
select *
into Dataset_SaleSummary
from [dbo].[weightsDashboard]
Thanks in advance.
The root cause Would be driver. Use ODBC instead of OLE DB.If you are trying to connect oracle database from sql server
Delete the linked server from SQL Server
Right click on the "Linked Servers" folder and select "New Linked Server..."
Linked Server: enter anything..this will be the name of your new linked server
Provider: Select "Oracle Provider for OLE DB"
Product Name: enter "Oracle" (without the double quotes)
Data Source: enter the alias from your TNSNAMES.ORA file.
Provider String: leave it blank
Location: leave it blank
Catalog: leave it blank
Now go to the "Security" tab, and click the last radio button that says "Be made using this security context:" and enter the username & password for your connection.
I had exact same problem with an SQL 2014 getting data from SQL 2000 through OPENQUERY. Because ODBC compatibility problem, I had to keep generic OLE DB for ODBC driver. Moreover, the problem was only with SQL non-admin account. So finally, the solution I found was to add SET ROWCOUNT 0:
SELECT * FROM OPENQUERY(DWH_LINK, 'SET ROWCOUNT 0 SELECT * FROM TABLEA ')
It seems the rowcount might been change somewhere through the SQL procedure (or for this user session), so setting it to 0 force it to return "all rows".

How do I convert an Oracle TIMESTAMP data type to SQL Server DATETIME2 data type while connected via Link Server.?

I have tried some examples but so far not working.
I have a Link Server (SQL Server 2014) to an Oracle 12C Database.
The table contain a datatype TIMESTAMP with data like this:
22-MAR-15 04.18.24.144789000 PM
When attempting to query this table in SQL Server 2014 via link server I get the following error using this code:
SELECT CAST(OracleTimeStampColumn AS DATETIME2(7)) FROM linkServerTable
Error:
Msg 7354, Level 16, State 1, Line 8
The OLE DB provider "OraOLEDB.Oracle" for linked server "MyLinkServer" supplied invalid metadata for column "MyDateColumn". The data type is not supported.
While the error is self explanatory, I am not certain how to resolve this.
I need to convert the timestamp to datetime2. Is this possible?
You can work around this problem by using OPENQUERY. For me, connecting to Oracle 12 from SQL 2008 over a linked server, this query fails:
SELECT TOP 10 TimestampField
FROM ORACLE..Schema.TableName
...with this error:
The OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE" supplied invalid metadata for column "TimestampField". The data type is not supported.
This occurs even if I do not include the offending column (which is of type TIMESTAMP(6). Explicitly casting it to DATETIME does not help either.
However, this works:
SELECT * FROM OPENQUERY(ORACLE, 'SELECT "TimestampField" FROM SchemaName.TableName WHERE ROWNUM <= 10')
...and the data returned flows nicely into a DATETIME2() field.
One way to solve the problem is to create a view in oracle server and convert the OracleTimeStampColumn compatible with sql server's datetime2datatype. You can change the time format to 24 hours format in oracle server's view and mark the field as varchar. Then you can convert the varchar2 column to datetime2 when selecting the column in SQL Server.
In Oracle Server
Create or Replace View VW_YourTableName As
select to_char(OracleTimeStampColumn , 'DD/MM/YYYY HH24:MI:SS.FF') OracleTimeStampColumn from YourTableName
In SQL Server
SELECT CAST(OracleTimeStampColumn AS DATETIME2(7)) FROM **linkServerVIEW**

Resources