SQL Server OpenQuery to Exec command - sql-server

The following query is working fine in SQL Server 2008 R2
select *
from openquery(LinkedServerName, 'exec databaseName.dbo.SP_GET_INFO');
I'm trying to convert it using exec command, but no luck.
For instance:
exec ('call databaseName.dbo.SP_GET_INFO') at LinkedServerName
Always getting a syntax error with this message:
Could not execute statement on remote server 'LinkedServerName'
Thanks.

Thanks to #lad2025 for the great help.
Using the following script will work:
exec ('databaseName.dbo.SP_GET_INFO') at LinkedServerName
The first time I tested the code it did not work because of the driver I used to create the linked server.
I have used "Microsoft OLE DB Provider for ODBC Drivers". With this one I got the error:
OLE DB provider "STREAM" for linked server "(null)" returned message ...
The reason was because in the stored procedure I do a select on a nText column which has null values.
So, I changed the driver to "Microsoft OLE DB Provider for SQL Server" and the script worked fine.
Hope this helps someone else.

Related

Opening Data from Excel in SQL Server

I am trying to select some data from Excel into my SQL query.
I have the following query:
SELECT*
FROM
openrowset('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=S:\Nh\NHData.xlsx;',
'SELECT *
FROM [Pre Upgrade data$]') as t2
If I run this on my local SQL server instance, it works fine, however to get it to work, I had to follow the procedure outlined on the below link, setting the SQL server service to run as my user account.
http://www.aspsnippets.com/Articles/The-OLE-DB-provider-Microsoft.Ace.OLEDB.12.0-for-linked-server-null.aspx
Without changing the user, I would get the following error:
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
For obvious reasons I cannot change the SQL server user on my live SQL database server.
How can I get this to run?

"Could not Find Server in sys.servers" when deleting, but OK when selecting

We're currently having an issue with one of our linked servers.
if I run this code it works
SELECT *
FROM [LINKEDSERVER].[databasename].dbo.[tablename] a
INNER JOIN [localdb].dbo.[localtable] b
on b.somefield = a.somefield
But this fails
DELETE FROM a
FROM [LINKEDSERVER].[databasename].dbo.[tablename] a
INNER JOIN [localdb].dbo.[localtable] b
on b.somefield = a.somefield
giving error:
OLE DB provider "SQLNCLI10" for linked server <servername> returned message "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.".
Msg 7202, Level 11, State 2, Line 1
Could not find server <servername> in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
The linked server does appear in sys.sysservers, and I've tried dropping and readding the linked server, to no effect:
exec sp_dropserver <servername>, droplogins
exec sp_addserver <servername>
exec sp_serveroption <servername>, 'data access' , 'true'
Linked server is SQL 2005, linking server is 2008R2
In case this is relevant (and it seems possible), I recently migrated the SQL server to which we're linking to new hardware (with new SQL install etc.) and changed the IP of new hardware server back to that of old server for continuity. In all other respects this has worked fine. We never had this problem on the old server.

Importing XML file into SQL Server 2000 using OPENROWSET

I'm trying to import an XML fie into SQL Server 2000 (SP2) table. I tried below query and it's giving syntax error near the word BULK. Not entirely sure if BULK and SINGLE_BLOB work in SQL Server 2000 SP2.
SELECT * FROM OPENROWSET(BULK N'E:\temp\PersonData.xml', SINGLE_BLOB) AS x
Also tried below query....
SELECT * FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Text Driver (*.xml)};DefaultDir=E:\temp\PersonData.xml;',
'SELECT * FROM [PersonData.xml];' )
....and it gave this error:
[OLE/DB provider returned message: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified]
OLE DB error trace [OLE/DB Provider 'MSDASQL' IDBInitialize::Initialize returned 0x80004005: ].
What am I doing wrong here? or Is it a SQL server 2000 issue?. My goal is to import an XML file into SQL Server using a stored procedure. Can't upgrade the server nor can I use a third party tool. Within these boundaries, please suggest me a way to achieve this. Thanks in advance.
OPENROWSET(BULK ...) was introduced in SQL Server 2005, so you can't use it.
OPENROWSET('MSDASQL', ...) is notorious for giving unhelpful error messages. See Error: "OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Driver Manager] Data source name not found ..." for a related question; this suggests that the file name should not be part of the DefaultDir parameter (so DefaultDir=E:\temp).
If all else fails, you could use BULK INSERT with bogus values for the field and row terminators to read the entire file in a single NTEXT column; you can then use sp_xml_preparedocument and OPENXML to read it.

T-SQL, OPENROWSET copying tables between databases on the same 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')

how to undo sp_addlinkedserver abc,'SQL Server'?

Wow i was just playing around with sp_addlinkedserver and i accidentally ran this command: sp_addlinkedserver abc,'SQL Server'
1) i had command(s) completed successfully.. but what happened?
2) how do i undo what i did?
You created a link to a server named abc.
You could try to query the server across this link using a command such as:
select *
from abc.master.information_schema.tables
But (unless you really do have a server called abc) it'll return you a message similar to:
OLE DB provider "SQLNCLI10" for linked server "abc" 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.".
You can view your linked server in SSMS under Server Objects>>Linked Servers in the Object Explorer.
To get rid of the linked server, use the following statement:
sp_dropserver abc
You now have a linked server called abc
To remove, use sp_dropserver (There is no sp_droplinkedserver). Thus:
EXEC sp_dropserver 'abc', 'droplogins'
You added the Linked Server, see here about using it. Briefly, Linked servers used to obtain the ability to make distributed queries between your and linked servers:
SELECT MyServer.MyDatabase.dbo.Table1.Field1,
LinkedServer.MyDatabase.dbo.Table2.Field2
FROM MyServer.MyDatabase.dbo.Table1
INNER JOIN LinkedServer.MyDatabase.dbo.Table2
ON MyServer.MyDatabase.dbo.Table1.ID=LinkedServer.MyDatabase.dbo.Table2.ID

Resources