Quantcast
Channel: Linked Server – SQL Authority with Pinal Dave
Viewing all 12 articles
Browse latest View live

SQL SERVER – How to Create Linked Server to PostgreSQL?

$
0
0

The databases world is expanding and I have been fortunate enough to learn and share my experiences around databases like MySQL, PostgreSQL apart from working with SQL Server. I always try to expand my horizon and try various database products. In case you don’t know, I have a course on Pluralsight about PostgreSQL.

It is always interesting to integrate various products and make them work seamlessly. Due to my knowledge of both database products, I have been asked one question very frequently.

How can I create linked servers in SQL connecting with Postgres?

Though this question looks simple and easy – I thought of writing a note to show you the actual steps to achieve the same. This blog shows a simple example about creating linked server. There is a provider called PGOLEDB which can be used for this purpose.

Create a Database and Table in PostgreSQL

  1. Create database:
CREATE DATABASE SQLAuthority WITH OWNER = postgres ENCODING = 'UTF8';
  1. Once database is created, change the connection, create table and insert some data.
CREATE TABLE MyTable 
( 
ID integer NOT NULL, 
Name varchar(128) NOT NULL
);
insert into MyTable values (1, 'Pinal Dave');

Verify that we have data in table

Select * from MyTable

SQL SERVER - How to Create Linked Server to PostgreSQL? PG-Linked-01

Create a Linked Server in SQL Server

  1. Go to http://www.pgoledb.com and choose “Download” from menu bar.
    SQL SERVER - How to Create Linked Server to PostgreSQL? PG-Linked-02
  2. Go to the page and choose “PGNP OLEDB Providers for Postgres, Greenplum and Redshift” as shown below.
    SQL SERVER - How to Create Linked Server to PostgreSQL? PG-Linked-03
  3. Install it and then we should see provider it in SSMS
    SQL SERVER - How to Create Linked Server to PostgreSQL? PG-Linked-04
  1. Modify and run below script in SQL Server. You need to change Server Name, port etc.
    -- Change parameter for provider Allow In Procees = true / DynamicParameters = true
    EXEC MASTER.dbo.sp_MSset_oledb_prop N'PGNP'  ,N'AllowInProcess',1
    GO
    EXEC MASTER.dbo.sp_MSset_oledb_prop N'PGNP'  ,N'DynamicParameters',1
    GO
    DECLARE @name NVARCHAR(4000);
    DECLARE @provider NVARCHAR(4000);
    DECLARE @servername NVARCHAR(4000);
    DECLARE @port NVARCHAR(4000);
    DECLARE @db_name NVARCHAR(4000)
    -- destination postgres database
    SET @name = N'SQLAuth_PG';
    SET @provider = N'PGNP';
    SET @servername = N'localhost';
    SET @port = 'PORT=5432;'
    SET @db_name = N'sqlauthority';
    -- create linked server
    EXEC MASTER.dbo.sp_addlinkedserver @server = @name
    ,@srvproduct = N'PGNP'
    ,@provider = N'PGNP'
    ,@datasrc = @servername
    ,@provstr = @port
    ,@catalog = @db_name
    -- username and password for postgres
    EXEC MASTER.dbo.sp_addlinkedsrvlogin @rmtsrvname = @name
    ,@useself = N'False'
    ,@locallogin = NULL
    ,@rmtuser = N'postgres'
    ,@rmtpassword = 'sa'
    -- set up Extended properties of the Linked Server
    EXEC MASTER.dbo.sp_serveroption @server = @name
    ,@optname = 'data access'
    ,@optvalue = 'true'
    EXEC MASTER.dbo.sp_serveroption @server = @name
    ,@optname = 'use remote collation'
    ,@optvalue = 'true'
    EXEC MASTER.dbo.sp_serveroption @server = @name
    ,@optname = 'rpc'
    ,@optvalue = 'true'
    EXEC MASTER.dbo.sp_serveroption @server = @name
    ,@optname = 'rpc out'
    ,@optvalue = 'true'
    GO
    

     

  2. Once completed, we should be able to see linked server created as shown below.
    SQL SERVER - How to Create Linked Server to PostgreSQL? PG-Linked-05

Test the Linked Server

Here are the test queries which I have used based on earlier script.

-- USING FOUR PART NAMING
SELECT [id],[name]
FROM   [SQLAuth_PG].[sqlauthority].[public].[mytable]
GO
-- USING OPENQUERY
SELECT *
FROM OPENQUERY(SQLAuth_PG, 'select id, name from mytable');

SQL SERVER - How to Create Linked Server to PostgreSQL? PG-Linked-06

Have you ever created any other 3rd party linked server in your environments? What methods have you used? Will you be kind enough to share the same via comments?

Reference : Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – How to Create Linked Server to PostgreSQL?


SQL SERVER – FIX – Linked Server Error 7399 Invalid authorization specification

$
0
0

I have personally seen when people use Linked Server there are a number of issues from authentication to performance. These issues are part of working with something that has so many variations and permutations of actually going wrong. Many a times I suggest people to keep away from it as much as possible. But it cannot happen all the times and there are code blocks in their application that relies on this as a requirement.

This is one of the common error I see in various forums. When someone is new to SQL Server and tried to create a linked server, he/she would just give server name and choose SQL Server as server type and hit OK. But SSMS would provide below error

SQL SERVER - FIX – Linked Server Error 7399 Invalid authorization specification linked-error-01

TITLE: Microsoft SQL Server Management Studio
——————————
The linked server has been created but failed a connection test. Do you want to keep the linked server?
——————————
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
——————————
The OLE DB provider “SQLNCLI11” for linked server “BIGPINAL” reported an error. Authentication failed.
Cannot initialize the data source object of OLE DB provider “SQLNCLI11” for linked server “BIGPINAL”.
OLE DB provider “SQLNCLI11” for linked server “BIGPINAL” returned message “Invalid authorization specification”. (Microsoft SQL Server, Error: 7399)
——————————
BUTTONS:
&Yes
&No
——————————

Above message has two messages

  1. A Question: The linked server has been created but failed a connection test. Do you want to keep the it?
  2. An Error: Microsoft SQL Server, Error: 7399 – The OLE DB provider “%ls” for linked server “%ls” reported an error. %ls

We can click Yes and linked server would be created but it won’t work. Once we click on Test Connection as shown below – we would get same error what we got during creation.

To fix this problem, we need to understand little about connectivity and authentication. Whenever we connect to SQL, there are two ways

  1. SQL Authentication: To use this, we need to provide login name and password while connecting to SQL Server. These account are stored within SQL Server.
  2. Windows Authentication: While using Windows operating system, we can use logged in account to SQL and it would allow us to connect to SQL Server without providing password because we have already logged into operating system and have been authenticated by Windows.

When the linked server is created, the default values under security is “Be made without using a security context” – this is the cause of the problem.

Solution: Choose one of the two from below highlighted.

SQL SERVER - FIX – Linked Server Error 7399 Invalid authorization specification linked-error-02

Have you seen this earlier? How would you provide windows account here? What are your opinion? Do let me know via comments below.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – FIX – Linked Server Error 7399 Invalid authorization specification

SQL SERVER – FIX – Linked Server Error 7416 – Access to the remote server is denied because no login-mapping exists

$
0
0

Last time I wrote a blog about linked server creation issue. As I said, these are one of the most common issues. But as soon as such blogs get released, I get a number of requests around them immediately. Here is the blog post which is discussing about the linked server error.

SQL SERVER – FIX – Linked Server Error 7399 Invalid authorization specification

After reading that one of the readers contacted me and told that he is getting below error.

SQL SERVER - FIX - Linked Server Error 7416 - Access to the remote server is denied because no login-mapping exists err-7416-01

On first look, I thought this was similar to what was published earlier. But things can surely turn out to be different. Since I had recently blogged about it, I thought to investigate this. To understand more, I looked at the error message in detail. It looks like:

TITLE: Microsoft SQL Server Management Studio
——————————
The linked server has been created but failed a connection test. Do you want to keep the linked server?
——————————
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
——————————
Access to the remote server is denied because no login-mapping exists. (Microsoft SQL Server, Error: 7416)
——————————

I played around with the linked server settings on the PC just to realize – I was able to reproduce the error by choosing 1st option in security tab. This was simpler than what I thought.

SQL SERVER - FIX - Linked Server Error 7416 - Access to the remote server is denied because no login-mapping exists err-7416-02

So, I asked him to use either option 2 or option 3. After using right option, the error message disappeared.

Do you remember seeing any such linked server error? Can you share your experience to what happened here this time? Leave a comment as we can learn from each other.

Reference: Pinal Dave (http://blog.sqlauthority.com)

First appeared on SQL SERVER – FIX – Linked Server Error 7416 – Access to the remote server is denied because no login-mapping exists

SQL SERVER – FIX: Msg 7356, Level 16 – The OLE DB provider “ASEOLEDB” for linked server “SYBASESERVER” supplied inconsistent metadata for a column

$
0
0

SQL SERVER - FIX: Msg 7356, Level 16 - The OLE DB provider "ASEOLEDB" for linked server "SYBASESERVER" supplied inconsistent metadata for a column close In SQL Server, we can create linked server to many other RDBMS. The providers to connect would generally publish by destination RBMS. In this client scenario, they were dealing with linked server to Sybase. Let us learn about the error The OLE DB provider “ASEOLEDB” for linked server “SYBASESERVER” supplied inconsistent metadata for a column.

Here is the query which was tried by the client.

SELECT *
FROM SYBASESERVER.prodxc.dbo.sysobjects

It was failing with below error

Msg 7356, Level 16, State 1, Line 1
The OLE DB provider “ASEOLEDB” for linked server “ALTAIR” supplied inconsistent metadata for a column. The column “versionts” (compile-time ordinal 20) of object “prodxc.dbo.sysobjects” was reported to have a “DBCOLUMNFLAGS_ISFIXEDLENGTH” of 16 at compile time and 0 at run time.

Linked Server setup was as below:

  • Destination Server: Sybase Server:  SYBASESERVER:5000
  • Provider: SAP ASE OLE DB Provider

To troubleshoot, I asked them to execute the above query by changing the table name with some other tables within the system catalog.  Interestingly, we were able to successfully execute those queries with no issues.  So, the issue seems to be related to the specific table sysobjects.

Instead of four-part naming, we asked to try below OPENQUERY format of the linked server query.

SELECT *
FROM OPENQUERY([SYBASESERVER], 'SELECT * FROM PRODXC.DBO.SYSOBJECTS')

Later we found that It appears that the Sybase OLE DB MDA provider on the Sybase server were not up to date.

WORKAROUND/SOLUTION

  1. Instead of four-part naming, use OPENQUERY
  2. Make sure that drivers on the client and server are up-to-date.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – FIX: Msg 7356, Level 16 – The OLE DB provider “ASEOLEDB” for linked server “SYBASESERVER” supplied inconsistent metadata for a column

SQL SERVER – Quickest Way to Add LoopBack Linked Server for OpenQuery

$
0
0

Here is the question which I received the other day from a user who was trying to add a linked server pointing its own server as he wanted to use OpenQuery. Let us read his question in details.

SQL SERVER - Quickest Way to Add LoopBack Linked Server for OpenQuery linkedserver

“Hey Pinal,

I just read your earlier blog post here – How to Insert Results of Stored Procedure into a Temporary Table? , I noticed that you have used OPENROWSET. Similarly, I want to use OPENQUERY for my stored procedure which is stored locally and I want to specify the name of the linked server.

Do you know any method by using which I can quickly create a linked server for my machine. Please note that I want to create a loopback linked server so it connects back to the same server, where it is created.

Fantastic question, indeed. Here is the quick and simple way to create a loopback linked server for your own server.

EXEC master.dbo.sp_addlinkedserver @server = N'loopback', @srvproduct=N'',
 @provider=N'SQLNCLI', @datasrc=@@SERVERNAME

That’s it. You are done!

By default the data access option will be enabled for your server. If due to any reason it is disabled, you can run following command and make sure that data access option for your server is enabled.

EXEC master.dbo.sp_serveroption @server=N'loopback',
@optname=N'data access', @optvalue=N'true'

I hope this simple blog post to help everyone who is trying to build a loopbac server.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – Quickest Way to Add LoopBack Linked Server for OpenQuery

SQL SERVER – System Procedure to List Out Table From Linked Server

$
0
0

There is a system procedure named sp_tables which is used to list out the tables available in the CURRENT database of the CURRENT server. But did you know that there exists another system stored procedure that can be used to list out the tables of database available in the linked server?

SQL SERVER - System Procedure to List Out Table From Linked Server systemspforlinkedserver

You can use the system stored procedure named sp_tables_ex.

The following returns list of tables available in the specified Server and database.

EXEC sp_tables_ex
@table_server                     = 'your_linked_server_name'
, @table_catalog                  = 'your_database'
, @table_type                     = 'TABLE'

You can also use the same stored procedure to know the list of Views.

EXEC sp_tables_ex
@table_server    = 'your_linked_server_name'
, @table_catalog = 'your_database'
, @table_type    = 'VIEW'

I would love to know if you have used any of the SP for your remote linked server. Please leave a comment and let me know.

Here are a few additional resources:

SQL SERVER – System Stored Procedures I Use to Get Started
SQL SERVER – System procedures to know SQL Server Version
SQL SERVER – How to use Procedure sp_user_counter1 to sp_user_counter10
SQL SERVER – Stored Procedures Advantages and Best Advantage

I believe the biggest advantage of the stored procedure is that it saves lots of network bandwidth conservation. It has been a long time since I have used views or triggers since I am very comfortable with Stored Procedures.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – System Procedure to List Out Table From Linked Server

SQL SERVER – How to Create Linked Server to SQL Azure Database?

$
0
0

If you take the normal approach to create a linked server to SQL Azure Database, then you would end up in below error message.

SQL SERVER - How to Create Linked Server to SQL Azure Database? azure-ls-01

Here is the text of the error message

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

Reference to database and/or server name in ‘sqlauthority.sys.sp_tables_rowset2’ is not supported in this version of SQL Server. (Microsoft SQL Server, Error: 40515)

SOLUTION

For me, here is the configuration in Azure

Server name – sqlauthority.database.windows.net
Database name – sqlauthority

SQL SERVER - How to Create Linked Server to SQL Azure Database? azure-ls-02

Here are the steps I have taken to create linked server. I went to new linked server and on “General”, I used below:

  • Linked Server (name): LinkedServerName
  • Provider: Microsoft OLE DB Provider for SQL Server
  • Product name: (blank)
  • Data Source: azure_db_server.database.windows.net
  • Provider string: (blank)
  • Location: (blank)
  • Catalog: database name in Azure

SQL SERVER - How to Create Linked Server to SQL Azure Database? azure-ls-03

Then on “Security” tab, I used below.

  • Be made using this security context
    • Remote login: azure-database-user-name
    • With password: password

SQL SERVER - How to Create Linked Server to SQL Azure Database? azure-ls-04

Once done, hit OK.

Here is the information via linked server

SQL SERVER - How to Create Linked Server to SQL Azure Database? azure-ls-05

This is same what we saw when we were directly connected. I hope this blog can help you in fixing linked server errors to SQL Azure Database. Let me know if you have ever faced the same situation before. I would love to know your feedback in the comments.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – How to Create Linked Server to SQL Azure Database?

SQL SERVER – FIX: Number Data Type from Oracle Linked Sever Shown in Scientific Notation

$
0
0

SQL SERVER - FIX: Number Data Type from Oracle Linked Sever Shown in Scientific Notation anothererror One of my clients contacted me for an issue where they reported that they see a problem with numbers when they are accessed via Oracle linked server.

Here is the query which they used to demonstrate the issue.

IF (OBJECT_ID('TEMPDB..#TempDbTempTable') IS NOT NULL)
BEGIN
DROP TABLE #TempDbTempTable;
END
GO
CREATE TABLE #TempDbTempTable (
OneColumn NVARCHAR(500)
)
GO
INSERT INTO #TempDbTempTable (Value) 
select somenumber from
OPENROWSET('OraOLEDB.Oracle',
'(DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=dbsrv.domain.com) 
(PORT=1526)) (CONNECT_DATA=(SERVICE_NAME=ORAFIN)))';'scott';'tiger', 
'select 1234567891011121314151617181920 AS somenumber from dual')
GO
SELECT * FROM #TempDbTempTable
GO
DROP TABLE #TempDbTempTable
GO

When the run query on one server it returns 1.23457e+030 but on another SQL Server, it was showing 1234567891011121314151617181920. It was interesting to note that both servers have same Oracle linked server. We tried various tests and found that numbers which are from 6 digits or above to scientific notation.

We started finding a difference and found that the version of SQL was different.

WORKAROUND/SOLUTION

While searching on the internet, I was able to find an explanation of this behavior from Microsoft knowledge base article 3051993. (FIX: The value of NUMBER type is truncated when you select data from an Oracle-linked server by using OLE DB provider)

To fix the issue, we needed to enable trace flag 7314. You can read the blog to know the steps to let the trace flag. SQL SERVER – What is Trace Flag – An Introduction

Other possible workarounds would be to modify the query and use to_char around the number. This would cause SQL to treat this as a character rather than a number.

Have you faced any such issue in your organization?

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – FIX: Number Data Type from Oracle Linked Sever Shown in Scientific Notation


SQL SERVER – OLE DB Provider ‘Microsoft.ACE.OLEDB.12.0’ for Linked Server ‘(null)’ Returned Message ‘Unspecified Error’

$
0
0

SQL SERVER - OLE DB Provider 'Microsoft.ACE.OLEDB.12.0' for Linked Server '(null)' Returned Message 'Unspecified Error' oledberror There are many situations where you have to create a linked server to Microsoft Excel and read data from there. While doing that, I encountered an error and in this blog, we would discuss how to fix OLE DB provider ‘Microsoft.ACE.OLEDB.12.0’ for linked server ‘(null)’ returned message ‘Unspecified error’

Below query can read data from excel in a shared location.

SELECT * FROM OPENROWSET(
'Microsoft.ACE.OLEDB.12.0'
,'Excel 12.0;Database=\\\\FileServer\\ExcelShare\\HRMSDATA.xlsx;HDR=YES;IMEX=1'
,'SELECT * FROM [EMPMASTER$]')

This was failing with error:

OLE DB provider ‘Microsoft.ACE.OLEDB.12.0’ for linked server ‘(null)’ returned message ‘Unspecified error’.
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider ‘Microsoft.ACE.OLEDB.12.0’ for linked server ‘(null)’.

SOLUTION/WORKAROUND

Based on my search on the internet, we were not seeing DisallowAdHocAccess registry key under.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL14.MSSQLSERVER\Providers\Microsoft.ACE.OLEDB.12.0

We then executed below command and key got created automatically.

USE [master]
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DisallowAdHocAccess', 0
GO

After this, we were able to read data from excel without any error.

If you find some other solution, please share via comments.

Here are a few other blog post related to the same subject:

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – OLE DB Provider ‘Microsoft.ACE.OLEDB.12.0’ for Linked Server ‘(null)’ Returned Message ‘Unspecified Error’

SQL SERVER – FIX: Msg 8180 – Statement(s) Could not be Prepared. Deferred Prepare Could not be Completed

$
0
0

While running a linked server query, I encountered an error and learned something new. In this blog we would learn about how to fix error – Msg 8180 – Statement(s) could not be prepared.

Here is the complete error message which I received.

OLE DB provider “SQLNCLI11” for linked server “SQL2019” returned message “Deferred prepare could not be completed.”.
Msg 8180, Level 16, State 1, Line 13
Statement(s) could not be prepared.
Msg 4104, Level 16, State 1, Line 13
The multi-part identifier “NAME.ID” could not be bound.

Reproducing the error is very easy. In my case, I created a linked server (called as SQL2019) and ran below query.

SELECT *
FROM OPENQUERY([SQL2019],'SELECT NAME . ID FROM SYS.DATABASES')

SQL SERVER - FIX: Msg 8180 - Statement(s) Could not be Prepared. Deferred Prepare Could not be Completed linked-stmt-prep-01

WORKAROUND/SOLUTION

When I captured profiler, I was able to understand the meaning of it. We can see below in profiler.

SQL SERVER - FIX: Msg 8180 - Statement(s) Could not be Prepared. Deferred Prepare Could not be Completed linked-stmt-prep-02

The statement which came to the linked server was

declare @p1 int
set @p1=0
exec sp_prepare @p1 output,NULL,N'SELECT NAME . ID FROM SYS.DATABASES',1
select @p1

and that failed as seen in profiler.

The message essentially means that the statement could not be compiled on the destination server. Based on my search on the internet, we should see the real error at the end of multiple messages. In this situation the error is

Msg 4104, Level 16, State 1, Line 13
The multi-part identifier “NAME.ID” could not be bound.

Of course, I know the error. I have put dot instead of a comma to generate an error.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – FIX: Msg 8180 – Statement(s) Could not be Prepared. Deferred Prepare Could not be Completed

SQL SERVER – FIX: Msg 15274 – Access to the Remote Server is Denied Because the Current Security Context is not Trusted

$
0
0

While working with a client, I encountered few errors while using linked server and I am going to share my solutions via a few blog posts. In this blog we would talk about fixing error 15274 – Access to the remote server is denied because the current security context is not trusted.

Before I talk about the problem and the solution, I must share a few environment details.

Remote Server Error

In my lab, I got two servers SQLSERVER-0 and SQLSERVER-1. The linked server was created on SQLSERVER-1 which was given name as ONE, connecting to SQL SERVER-1. There are two databases involved here. On source server (SQLSERVER-0) I have a database called SQLDB0 and on destination (SQLSERVER-1), a database is called SQLDB1.

  1. Linked server “test connection” was working just fine.
    SQL SERVER - FIX: Msg 15274 - Access to the Remote Server is Denied Because the Current Security Context is not Trusted link-srv-err1-01
  2. Linked server was not created using “SQL Server” option, but “Other data source” was used before application wanted to use a different name.
    SQL SERVER - FIX: Msg 15274 - Access to the Remote Server is Denied Because the Current Security Context is not Trusted link-srv-err1-02
  3. Simple queries were working fine but a stored procedure which was using “execute as user” was failing.

Here is the error which was coming when we were executing a stored procedure. Here is an oversimplified version of the stored procedure. The procedure is created in database SQLDB0.

CREATE PROCEDURE usp_fetch_data
AS
BEGIN
	EXECUTE AS user = 'user_no_login'
	SELECT *
	FROM One.SQLDB1.dbo.Table_1
	REVERT
END

And here is the error message when I execute it as below.

Here is the text of the error message.

Msg 15274, Level 16, State 1, Procedure usp_fetch_data, Line 5 [Batch Start Line 9]
Access to the remote server is denied because the current security context is not trusted.
I captured profiler trace but found nothing interesting.  Since error message was talking about “TRUSTED”, I recalled TRUSTWORTHY property of the database.

WORKAROUND/SOLUTION

My feeling was correct. As soon as I changed the database property on the source database, the above error disappeared. Here is the T-SQL to check the property.

SELECT is_trustworthy_on, name 
FROM sys.databases
WHERE name = 'SQLDB0'
GO

If you see is_trustworthy_on set as 0 (ZERO) then run below command to enable it and make it 1 (ONE).

ALTER DATABASE SQLDB0 SET TRUSTWORTHY ON
GO

Have you seen a similar error? Did you find any other solution?

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – FIX: Msg 15274 – Access to the Remote Server is Denied Because the Current Security Context is not Trusted

SQL SERVER – Linked Server Error – Msg 3910 – Transaction Context In Use By Another Session

$
0
0

One of my clients contacted me for an error that appeared while performing a distributed transaction over a linked server. The interesting part was that the linked server was local SQL Server, this is also called a loopback linked server. In this blog, I would share my understanding and the solution of Linked Server Error – Msg 3910 – Transaction context in use by another session.

SQL SERVER - Linked Server Error - Msg 3910 - Transaction Context In Use By Another Session Transaction-context-800x284

The situation with my client was that when he runs a query that performs a distributed transaction, it fails. Here is the screenshot of the behavior.

SQL SERVER - Linked Server Error - Msg 3910 - Transaction Context In Use By Another Session local-linked-srv-01

Here is the text of the error message.

Msg 3910, Level 16, State 2, Line 1
Transaction context in use by another session.

In the above screenshot, the IP used for the linked server was the IP address for the local machine.

If you ever try to create a linked server for a local server, you will get an error message.

SQL SERVER - Linked Server Error - Msg 3910 - Transaction Context In Use By Another Session local-linked-srv-02

Text: You cannot create a local SQL Server as a linked server.

WORKAROUND/SOLUTION

Here are a few things we should check if we get this error message.

  1. Make sure SELECT @@SERVERNAME is giving the right server name.
  2. Make sure SELECT * FROM SYS.SERVERS is showing local server name under server_id = 0

If you must fetch data from the local server, there is no real need for creating a linked server. You can use three-part naming (database.schema.object) instead of four-part naming (server.database.schema.object)

I was able to locate old documentation here which says Loopback linked servers cannot be used in a distributed transaction.

Please comment and let me know if you find some other solution. Here are a few additional blog posts which you may find interesting.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Linked Server Error – Msg 3910 – Transaction Context In Use By Another Session

Viewing all 12 articles
Browse latest View live