T-SQL, OPENROWSET copying tables between databases on the same server - sql-server

I am developping some code that transfers data between SQL servers. In this phase, all my work is on the same server (local, where I am the owner), but I am already trying to implement the OPENROWSET functionnalities that will be used at production time (where data will be on different servers and where I will have to build queries for the transfers). The following 3 codes are supposed to do the very same thing, but the one making use of OPENROWSET is giving me an error ... Bref, I am stuck! if anyone could help...
3 parts naming: works
USE db1
SELECT * INTO dbo.myTable FROM db2.dbo.myTable
OPENDATASOURCE: works
USE db1
SELECT * INTO dbo.myTable FROM OPENDATASOURCE
('SQLOLEDB',
'Data Source=127.0.0.1\SQLEXPRESS;Integrated Security=SSPI'
).db2.dbo.myTable
OPENROWSET: does not work
USE db1
SELECT * INTO dbo.myTable FROM OPENROWSET
('SQLOLEDB',
'Trusted_Connection=yes;Server=(Local)',
'db2.dbo.myTable')
Where I am getting the following message:
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "Login timeout expired".
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.".
Of course I have tried to use the standard properties of my connection string (as with OPENDATASOURCE) but I was also getting the following error:
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "Invalid authorization specification".
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "Invalid connection string attribute".

For one thing: Your OPENROWSET statement isn't specifying the server instance.
Try
USE db1
SELECT * INTO dbo.myTable FROM OPENROWSET
('SQLOLEDB',
'Trusted_Connection=yes;Server=127.0.0.1\SQLEXPRESS',
'db2.dbo.myTable')

Related

SQLNCLI syntax error when using OPENROWSET in SQL Server 2019

SQLNCLI syntax when using OPENROWSET
We have been using this syntax when using OpenRowset to collect data from other SQL Server instances.
SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=MyServer;Database=Tasks; User Id=sa;Password=myPassword;','SELECT * FROM Patients') As a;
I am facing following some errors.
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "Invalid authorization specification".
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "Invalid connection string attribute".
The OLE DB provider "SQLNCLI" for linked server "(null)" reported an error. Authentication failed.
Cannot initialize the data source object of OLE DB provider "SQLNCLI11" for linked server "(null)".
I resolved my error using following query.
I replaced SQLCLI with MSDASQL and wrote driver name as SQL Server.
select a.* from openrowset('MSDASQL', 'Driver={SQL SERVER}; Server=MYINSTANCE;UID=mySQLUser; PWD=*******;', 'select * from sys.databases') as a

Issue when trying to UPDATE table on LINKED SERVER

I connected my MS SQL Server database to Firebird using odbc. When I use a SELECT statement it works perfectly. The error occurs when I try to UPDATE. I get this error message:
The OLE DB provider "MSDASQL" for linked server "server_name" could not update table
The strange thing is, it can update some other tables in the same db, but not a select few.

OLE DB provider "OraOLEDB.Oracle" for linked server returned message "ROW-00004: Invalid column datatype"

The following SQL running in SQL Server
select *
from openquery(oracle_ls,'select xmlelement("Test") from dual')
returns the error
OLE DB provider "OraOLEDB.Oracle" for linked server "oracle_ls" returned message "ROW-00004: Invalid column datatype".
How to return Oracle XML type via linked server?
OLEDB driver complains as the data type is not supported.
However when you convert the xml to string, it should work.
select * from openquery(oracle_ls,
'select xmlelement("Test").getstringval() from dual')

Call Oracle function with ref cursor output in SQL Server linked server?

How do I need to call an Oracle function with ref cursor output in a SQL Server linked server?
I used this statement:
SELECT *
FROM OPENQUERY(oracle, 'select * from functionname(''N'',''2016-11-01'')')
but I get this error
OLE DB provider "OraOLEDB.Oracle" for linked server "oracle" returned message "ORA-00933: SQL command not properly ended".
Msg 7321, Level 16, State 2, Line 33
An error occurred while preparing the query "select * from functionname('N','2016-11-01')" for execution against OLE DB provider "OraOLEDB.Oracle" for linked server "oracle".
Any help?
Maybe this will help?
Calling an Oracle function from SQL Server Linked Server
EXECUTE (Query, Parameters) AT LinkedServerName

Creating a view from another Server error

Hi All I am trying to create a View from another database server...
I have have got server A an din Server A is where i want to create a view referencing a table from Server B.... when I run the command below:
create view TableFromServerB as select * from ServerB.master.information_schema.table;
I get the following error:
OLE DB provider "SQLNCLI11" for linked server "ServerB" returned
message "Login timeout expired". OLE DB provider "SQLNCLI11" for
linked server "ServerB" returned message "A network-related or
instance-specific error has occurred while establishing a connection
to SQL Server. Server is not found or not accessible. Check if
instance name is correct and if SQL Server is configured to allow
remote connections. For more information see SQL Server Books
Online.". Msg 53, Level 16, State 1, Line 0 Named Pipes Provider:
Could not open a connection to SQL Server [53].
I have done some research online and i came to a source that suggested using sp_addlinkedserver in my command .. which i have done and when i did this it said server already exists . So is there anything i am missing out here??
I have seen an issue like this before:
Can you please do something like this:
Create a view in your linked server to access the object i.e master.information_schema.table;
Use this view in the queries from the server where you want to display the results.
Eg:
--- create view script--
create view dbo.vINFORMATION_SCHEMATABLES
as
SELECT *
FROM INFORMATION_SCHEMA.TABLES
go
-- select statement from local server
select * from [testSRV].[testDB_far].[dbo].[vINFORMATION_SCHEMATABLES]
Let me know if this helps.

Resources