Product: | DB Connectivity OLE DB Products |
Version: | All |
ID: | 1369 |
Summary: | T-SQL commands to create a linked server in SQL Server |
The T-SQL code to create a linked server is shown below.
Make sure you create a data link file and use it as the @datasrc. Make sure to include the extension “.udl” when you specify the data link file to be used.
EXEC sp_addLinkedServer
@server = 'TEST2',
@srvproduct = '',
@provider = 'HiTOLEDB400',
@datasrc = 'V5R1.udl',
@location = NULL,
@provstr = 'Port Number=8471;Rowset Locate=True;'
GO
EXEC sp_addlinkedsrvlogin
@rmtsrvname='TEST2',
@useself='false',
@rmtuser='AS400User',
@rmtpassword='AS400UserPWD'
GO
To drop the linked server using T-SQL:
EXEC sp_dropserver 'TEST2', 'droplogins'