Search code examples
sqlsql-serverstored-proceduressql-server-2012linked-server

Access another SQL Server through stored procedure


I want to access another SQL Server to import and update data of that server's database through a stored procedure.

For that I created linked server using sp_addlinkedserver.

EXEC sp_addlinkedserver 
    @server = N'LINKEDSERVER'
    ,@srvproduct=N''
    ,@provider=N'SQLOLEDB'
    ,@datasrc=N'[DB-Instance.cuprxxxlj.ap-xxxxxxx-x.rds.amazonaws.com]';

Then try to access by SQL query

SELECT * 
FROM [LINKEDSERVER].[DATABASE].[dbo].[TABLE]

And getting errors like

OLE DB provider "SQLNCLI11" for linked server "LINKEDSERVER" returned message "Login timeout expired".

OLE DB provider "SQLNCLI11" for linked server "LINKEDSERVER" 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 had created database instance on amazon server and another is also on the amazon server and both are publicly accessible but don't know why I'm getting Server is not found or not accessible. Am I missing any server site setting or something else?

Please guide me if I am going in wrong direction.


Solution

  • I found the answer. The problem was an instance, I had created "Amazon RDS for SQL Server" which is currently not supporting to "Linked servers".

    So I create a new AWS EC2 instance for Windows Server 2012 R2 and then I create Linked servers in it and tried to assess remotely. Now its working fine as I wanted.

    Here is detailed document to Running SQL Server Linked Servers on AWS

    Thanks.