Reputation: 6940
How to add linked server establishing connection from remote SQL Server linking to local instance of SQL Server Express? By local I mean the PC I use with SQL Server Express instance and by remote I mean server I connect to with SSMS. Mostly on the web the opposite situation is described - from remote to local. I start with that documentation: https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine
.\SQLExpress
or some other phrase. SQL Server
or Other data source
? Upvotes: 4
Views: 10047
Reputation:
Yes. It is local to you not to the other SQL Server where you want to create linked server. To the other server your local server is remote. Make sure you local server can accept network connections by using SQL Server Configuration Manager.
If you go with SQL Server, you have to provide network name of your local server - the name you use in SSMS. Unless you use localhost or "." then you need to find it out, you can use ipconfig /all
and look for Host Name. This will work assuming both your local server and the other server are on the same network.
With Other server you have more options. In linked server you provide whatever you like and then specify connection information bellow.
Here you can find more information about values
You choose what your scenario allows. Choose Sql Server if you can
Look at link in pt.2
EDIT:
DISCLAIMER I am not network engineer so I might be missing some crucial steps to actually make it work and this is just general direction.
If you are on other network than your other server you would have to open firewall on 1433 TCP port not only on your machine but also on the router and map router's port 1433 to your machine's port 1433. Then having your router public IP you should be able to link both servers. It kind of goes beyond scope of SO like @MatSnow mention in his comment.
Upvotes: 4
Reputation: 14341
As others have pointed out. A lot of the answer and success of it depends on the ability for the 2 machines to communicate. so is DNS configured? Firewalls? Can you ping? Can you resolve machine names? Can you otherwise communicate from one server to the other? The difference of creating a linked server from your local machine and a remote server and vice versa is actually just reversing the information. your Local machine still needs to be a server with the appropriate configuration and permissions granted, but if it is then it is just another SQL server. A linked server only relys on having 2 SQL servers that are able to communicate.
Because I often work remote (VPN) and servers cannot always resolve my machines DNS name I use the following script to create a linked server on a remote server to connect to my local machine via IP address I am connected to the server via SSMS with:
DECLARE @AutoExecute BIT = 1
DECLARE @LinkedServerName VARCHAR(50) = 'LinkedServername' --if null or blank the linked server will just get the instance name
DECLARE @InstanceAppend VARCHAR(45) = '\InstanceName' --make emptry string if none
DECLARE @user VARCHAR(20) = 'username'
DECLARE @password VARCHAR(20) = 'password'
DECLARE @SQL VARCHAR(MAX)
DECLARE @IPAddress VARCHAR(255)
DECLARE @Instance VARCHAR(300)
DECLARE @SrvName VARCHAR(300)
SELECT @IPAddress = client_net_address
,@Instance = client_net_address + ISNULL(@InstanceAppend,'')
FROM
sys.dm_exec_connections
WHERE Session_id = @@SPID;
SET @SrvName = CASE WHEN LEN(@LinkedServerName) > 0 THEN @LinkedServerName ELSE @Instance END
IF NOT EXISTS (SELECT * FROM sys.servers WHERE name = @SrvName)
BEGIN
IF (LEN(@LinkedServerName) > 0)
BEGIN
SET @SQL = 'EXECUTE master.dbo.sp_addlinkedserver @server = N''' + @LinkedServerName + ''',@provider=''SQLOLEDB'', @srvproduct=N'''', @datasrc=N''' + @Instance + ''''
END
ELSE
BEGIN
SET @SQL = 'EXECUTE master.dbo.sp_addlinkedserver @server = N''' + @Instance + ''', @srvproduct=N''SQL Server'''
END
SET @SQL = @SQL + CHAR(13) + 'EXECUTE master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N''' + @SrvName + ''',@useself=N''False'',@rmtuser=''' + @user + ''', @rmtpassword=''' + @password + ''''
END
ELSE
BEGIN
PRINT 'Linked Server Exists'
END
PRINT @SQL
IF (@AutoExecute = 1)
BEGIN
BEGIN TRY
EXECUTE (@SQL)
PRINT 'Executed Successfully'
END TRY
BEGIN CATCH
PRINT 'Failed To Execute'
;THROW
END CATCH
END
Upvotes: 1
Reputation: 3169
I will not talk about your IP, because it is clear that you must have a public/reachable IP it is a pre-requisite
And once it is done, you have to open the port 1433 in your firewall so connections are not blocked as you can see here
At this point you just have to add the linked server as usual, by IP/Instance
Upvotes: 1
Reputation: 109
If you have found "from remote to local" which is the opposite of what you want just do imagine your PC is the server and the server is the local PC and go one. Imagine you have 2 machines : machine1 and machine2. to add a link server on machine1 to machine2 : Just open the new link server page as above on the machine1. Linked server : (Machine2 IP address) (Selected) SQL server
Go to the security page and select : Be made using this security context .
and enter the username and password you have from SQL server instance on machine2.
Upvotes: 5