Inserting into a table from view create witha link server - sql-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".

Related

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.

SQL to SAS ODBC connection - truncation of NVARCHAR(max) but getting ERROR: The option dbtype is not a valid output data set option

I have a table stored in SQL Server Management Studio version 15.0.18369.0 and I have a working and established ODBC connection to SAS language program World Programming Software version 3.4.
Previous import/reading of this data has been successful but the operator of the SQL server may have recently converted their data type to NVARCHAR(max). It may also be due to a driver change (I got a new laptop and reinstalled what I thought was the exact same OBDC for SQl driver as I had before but who knows) I have 64-bit ODBC Driver 17 for SQL server.
The VARCHAR(max) type in SQL causes the data to only be 1 character long in every column.
I have tried to fix it by:
Adding the DB max text option to libname
libname odbclib odbc dsn="xxx" user="xxx" password="xxx" DBMAX_TEXT=8000;
This did nothing so I also tried to add DB type option:
data mydata (dbtype=(mycol='char(25)')) ;
set odbclib.'sql data'n
run;
And I get ERROR:
The option dbtype is not a valid output data set option.
I have also tried DBSASTYPE, and putting both options in the set statement and this yields the same error.
I also tried with proc SQL:
proc sql noprint;
CONNECT TO ODBC(dsn="xxx" user="xxx" password="xxx"); create table
extract(compress=no dbsastype=(mycol='CHAR(20)')) as select * from
connection to odbc ( select * from dbo.'sql data'n
); disconnect from odbc; quit;
And I get
NOTE: Connected to DB: BB64 (Microsoft SQL Server version 12.00.2148)
NOTE: Successfully connected to database ODBC as alias ODBC. 3915
create table extract(compress=no
dbsastype=(mycol='CHAR(20)')) as 3916 select *
from connection to odbc 3917 ( 3918 select *
from dbo.'sql data'n ERROR: The option dbsastype is not a valid
output data set option 3919 3920 ); 3921
disconnect from odbc; NOTE: ERRORSTOP was specified. The statement
will not be executed due to an earlier error NOTE: Statements not
executed because of errors detected 3922 quit;
One thing to try would be putting the DBTYPE or DBSASTYPE in the right place.
data mydata;
set odbclib.'sql data'n(dbtype=(mycol='char(25)'));
run;
It is an option that would go on the "set" not the "data" line.
I would also encourage you to contact the developers, as it seems like this is a bug in their implementation; or perhaps try a different ODBC SQL driver.
Maybe a workaround in the meanwhile is to use CONVERT in the passthrough?
proc sql noprint;
CONNECT TO ODBC(dsn="xxx" user="xxx" password="xxx"); create table
extract as select * from
connection to odbc ( select a,b,c,convert(NCHAR(20),mycol) from dbo.'sql data'n
); disconnect from odbc; quit;

Importing Excel Data into SQL Server 2008 R2 Express

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.

Get along with image sql server linked servers from PostgreSQL

When obtaining an image grabde with sql server linked servers from PostgreSQL, I get the following error: OLE DB provider 'MSDASQL' for linked server 'bd_acceso_ruisegip' returned data that does not match expected data length for column '[MSDASQL] . fot_imagen '. The data length (maximum) expected is 255 and the data returned is 38471.
Don't know if you were dealing with a bytea column but I was having the same problem. Found the answer in the configuring of the postrgres ODBC system dsn. Under the Options/Datasource-page 2 there is a option for bytea as LO. Clicked that and now it works like a champ.
I found a similar issue when replicating some Forum data from PostgreSQL to MSSQL, using the PostgreSQL 64-bit driver and a Linked Server (.
When I coded like this:...
select * into Post from OpenQuery(PostgreSQL_Test1, 'select * From public.post')
... the MSSQL table defaulted to a column size of nvarchar(4000).
My fix: First, run it once with a small limit on the number of rows copied:
select * into Post from OpenQuery(PostgreSQL_Test1, 'select * From public.post limit 10')
Next, right-click on the local Post table. Choose "Script table as drop and create"
In the create script, replace the size of the offending column with VARCHAR(MAX)
Next, create the table.
Then use:
Insert Post select * from OpenQuery(PostgreSQL_Test1, 'select * From public.post')
Hope that helps.
Your mileage may vary.

What could cause "invalid object name" when I'd expect "Could not find server 'dbtest' in sys.servers"

I'm using SQL Server 2008 R2 Web Edition, entirely within SQL Management Studio. I need to do a cross-server query but my query always returns "Invalid object name", regardless of whether the server is linked. Changing the server name returns "Could not find server", exactly as I would expect.
I've added and removed the linked database several times using sp_addlinkedserver and sp_dropserver, checked the sys.Servers table and I've tried various permutations of the queries. My guess is that it's another feature with the same 4-part name syntax as linked servers.
For example, the following return "Invalid object name"
SELECT TOP 10 * FROM dbtest.Web.dbo.tblProduct -- this is correct
SELECT TOP 10 * FROM dbtest.Web.dbo.tblProductFake
SELECT TOP 10 * FROM dbtest.WebFake.dbo.tblProduct
If I deliberately change the server name to something incorrect, the following return "Could not find server", as I would expect
SELECT TOP 10 * FROM dbtestFake.Web.dbo.tblProduct
SELECT TOP 10 * FROM dbtest1.Web.dbo.tblProduct
SELECT TOP 10 * FROM dbtst.Fake.dbo.tblProduct
Queries against another linked server from the same database either return results, or return "The OLE DB provider "SQLNCLI10" for linked server "dbx" does not contain the table" depending on whether the query is correct, again, all as I would expect.
What's even weirder is that I can get Management Studio to build the query for me following "Server Objects" > "Linked Servers" and so on, then clicking "Script Table as SELECT to new query window", but that query will still show "Invalid object name".

Resources