. . DNS? ? ? ? ? - . - , , SQL-. SQL-, .
Since I often work remotely (VPN), and servers cannot always resolve my computer name, I use the following script to create a linked server on a remote server to connect to the local machine by IP address. I am connected to the server via SSMS using:
DECLARE @AutoExecute BIT = 1
DECLARE @LinkedServerName VARCHAR(50) = 'LinkedServername'
DECLARE @InstanceAppend VARCHAR(45) = '\InstanceName'
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
source
share