Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login failed when querying linked server

I am trying to create a linked server in SQL Server:

--Create the link to server "uranium"
EXEC master.dbo.sp_addlinkedserver 
      @server = N'uranium', 
      @srvproduct=N'', 
      @provider=N'SQLNCLI'

--Add the catch-all login with SQL Server authentication
EXEC master.dbo.sp_addlinkedsrvlogin 
      @rmtsrvname=N'uranium',
      @useself=N'False',
      @locallogin=NULL,
      @rmtuser=N'BatteryStaple',
      @rmtpassword='Horsecorrect'

And it creates fine. But any attempt to query the linked server, e.g.:

SELECT * FROM uranium.Periodic.dbo.Users

results in

Msg 18456, Level 14, State 1, Line 1
Login failed for user 'BatteryStaple'.

Except i know the credentials are correct:

  • Login: BatteryStaple
  • Password: Horsecorrect

because i can login when i connect directly using SQL Server Management Studio, or any other technology that is able to connect to a database.

enter image description here

Bonus Reading

  • Login Failed for linked server (he forgot to call sp_addlinkedsrvlogin)
  • Why am I getting a “login failed” when creating this linked server? (he's trying to use integrated authentication)
  • MSDN Blogs: SQL Linked Server Query failed with “Login failed for user …” (he's trying to make integrated authentication work)

Note: New SQL Server 2014 install. Every existing SQL 2000, 2005, 2008, 2008 R2 can communicate to their uranium linked server. I'm certain it is related to Microsoft's frustrating broken by default policy.

like image 370
Ian Boyd Avatar asked Mar 01 '16 20:03

Ian Boyd


People also ask

How do I fix Login failed for NT Authority anonymous logon?

To summarize, The Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON' error in both our cases seems to be caused by a service not running and/or not on the right user. Ensuring the right SPN or other Service is running and under the correct user should solve the anonymous part of the problem.

How do I run a query on a linked server?

Right-click on the Linked Server node and choose New Linked Server. In the General tab of the New Linked Server window, choose a name for your linked server, then choose the type of the server you need to connect to using that linked server.


2 Answers

The issue is that the SQL Server Management Studio interface creates the linked server using the OLEDB Provider:

enter image description here

This is equivalent to the original T-SQL:

--Create the link to server "uranium"
EXEC master.dbo.sp_addlinkedserver 
      @server = N'uranium', 
      @srvproduct=N'', @provider=N'SQLNCLI'

The fix is to create the linked server as SQL Server:

--Create the link to SQL Server "uranium"
EXEC master.dbo.sp_addlinkedserver 
      @server = N'uranium', 
      @srvproduct=N'SQL Server'

Shouldn't matter. Probably a regression in Microsoft SQL Server 2014 12.0.4213.0. Might be fixed in a service pack - if there is one.

But there it is; solved.

like image 58
Ian Boyd Avatar answered Oct 06 '22 00:10

Ian Boyd


Old post, but might be useful still. In my case it was that only Windows Authentication was set. Setting authentication for both Windows and SQL Server on the linked server fixed it.

like image 29
ashilon Avatar answered Oct 06 '22 00:10

ashilon