Share via


sp_addlinkedserver (Transact-SQL)

Creates a linked server. A linked server allows for access to distributed, heterogeneous queries against OLE DB data sources. After a linked server is created by using sp_addlinkedserver, distributed queries can be run against this server. If the linked server is defined as an instance of SQL Server, remote stored procedures can be executed.

Topic link iconTransact-SQL Syntax Conventions

Syntax

sp_addlinkedserver [ @server= ] 'server' [ , [ @srvproduct= ] 'product_name' ] 
     [ , [ @provider= ] 'provider_name' ]
     [ , [ @datasrc= ] 'data_source' ] 
     [ , [ @location= ] 'location' ] 
     [ , [ @provstr= ] 'provider_string' ] 
     [ , [ @catalog= ] 'catalog' ] 

Arguments

  • [ @server= ] 'server'
    Is the name of the linked server to create. server is sysname, with no default.
  • [ @srvproduct= ] 'product_name'
    Is the product name of the OLE DB data source to add as a linked server. product_name is nvarchar(128), with a default of NULL. If SQL Server, provider_name, data_source, location, provider_string, and catalog do not have to be specified.
  • [ @provider= ] 'provider_name'
    Is the unique programmatic identifier (PROGID) of the OLE DB provider that corresponds to this data source. provider_name must be unique for the specified OLE DB provider installed on the current computer. provider_name is nvarchar(128), with a default of NULL; however, if provider_name is omitted, SQLNCLI is used. SQLNCLI is the SQL Native Client OLE DB provider. The OLE DB provider is expected to be registered with the specified PROGID in the registry.
  • [ @datasrc= ] 'data_source'
    Is the name of the data source as interpreted by the OLE DB provider. data_source is nvarchar(4000). data_source is passed as the DBPROP_INIT_DATASOURCE property to initialize the OLE DB provider.
  • [ @location= ] 'location'
    Is the location of the database as interpreted by the OLE DB provider. location is nvarchar(4000), with a default of NULL. location is passed as the DBPROP_INIT_LOCATION property to initialize the OLE DB provider.
  • [ @provstr= ] 'provider_string'
    Is the OLE DB provider-specific connection string that identifies a unique data source. provider_string is nvarchar(4000), with a default of NULL. provstr is either passed to IDataInitialize or set as the DBPROP_INIT_PROVIDERSTRING property to initialize the OLE DB provider.

    When the linked server is created against the SQL Native Client OLE DB provider, the instance can be specified by using the SERVER keyword as SERVER=servername\instancename to specify a specific instance of SQL Server. servername is the name of the computer on which SQL Server is running, and instancename is the name of the specific instance of SQL Server to which the user will be connected.

    Note

    To access a mirrored database, a connection string must contain the database name. This name is necessary to enable failover attempts by the data access provider. The database can be specified in the @provstr or @catalog parameter. Optionally, the connection string can also supply a failover partner name. For more information, see Making the Initial Connection to a Database Mirroring Session.

  • [ @catalog= ] 'catalog'
    Is the catalog to be used when a connection is made to the OLE DB provider. catalog is sysname, with a default of NULL. catalog is passed as the DBPROP_INIT_CATALOG property to initialize the OLE DB provider. When the linked server is defined against an instance of SQL Server, catalog refers to the default database to which the linked server is mapped.

Return Code Values

0 (success) or 1 (failure)

Result Sets

None.

Remarks

The following table shows the ways that a linked server can be set up for data sources that can be accessed through OLE DB. A linked server can be set up more than one way for a particular data source; there can be more than one row for a data source type. This table also shows the sp_addlinkedserver parameter values to be used for setting up the linked server.

Remote OLE DB data source OLE DB provider product_name provider_name data_source location provider_string catalog

SQL Server

Microsoft SQL Native Client OLE DB Provider

SQL Server 1 (default)

 

 

 

 

 

SQL Server

Microsoft SQL Native Client OLE DB Provider

 

SQLNCLI

Network name of SQL Server (for default instance)

 

 

Database name (optional)

SQL Server

Microsoft SQL Native Client OLE DB Provider

 

SQLNCLI

servername\instancename (for specific instance)

 

 

Database name (optional)

Oracle

Microsoft OLE DB Provider for Oracle

Any2

MSDAORA

SQL*Net alias for Oracle database

 

 

 

Oracle, version 8 and later

Oracle Provider for OLE DB

Any

OraOLEDB.Oracle

Alias for the Oracle database

 

 

 

Access/Jet

Microsoft OLE DB Provider for Jet

Any

Microsoft.Jet.OLEDB.4.0

Full path of Jet database file

 

 

 

ODBC data source

Microsoft OLE DB Provider for ODBC

Any

MSDASQL

System DSN of ODBC data source

 

 

 

ODBC data source

Microsoft OLE DB Provider for ODBC

Any

MSDASQL

 

 

ODBC connection string

 

File system

Microsoft OLE DB Provider for Indexing Service

Any

MSIDXS

Indexing Service catalog name

 

 

 

Microsoft Excel Spreadsheet

Microsoft OLE DB Provider for Jet

Any

Microsoft.Jet.OLEDB.4.0

Full path of Excel file

 

Excel 5.0

 

IBM DB2 Database

Microsoft OLE DB Provider for DB2

Any

DB2OLEDB

 

 

See Microsoft OLE DB Provider for DB2 documentation.

Catalog name of DB2 database

1 This way of setting up a linked server forces the name of the linked server to be the same as the network name of the remote instance of SQL Server. Use data_source to specify the server.

2 "Any" indicates that the product name can be anything.

The Microsoft SQL Native Client OLE DB provider (SQLNCLI) is the provider that is used with SQL Server 2005 if no provider name is specified or if SQL Server is specified as the product name. Even if you specify the older provider name, SQLOLEDB, it will be changed to SQLNCLI when persisted to the catalog.

The data_source, location, provider_string, and catalog parameters identify the database or databases the linked server points to. If any one of these parameters is NULL, the corresponding OLE DB initialization property is not set.

In a clustered environment, when you specify file names to point to OLE DB data sources, use the universal naming convention name (UNC) or a shared drive to specify the location.

sp_addlinkedserver cannot be executed within a user-defined transaction.

ms190479.security(en-US,SQL.90).gifSecurity Note:
When a linked server is created by using sp_addlinkedserver, a default self-mapping is added for all local logins. For non-SQL Server providers, SQL Server Authenticated logins may be able to gain access to the provider under the SQL Server service account. Administrators should consider using sp_droplinkedsrvlogin <linkedserver_name>, NULL to remove the global mapping.

Permissions

Requires ALTER ANY LINKED SERVER permission.

Examples

A. Using the Microsoft SQL Native Client OLE DB Provider

The following example creates a linked server named SEATTLESales. The product name is SQL Server, and no provider name is used.

USE master;
GO
EXEC sp_addlinkedserver 
   'SEATTLESales',
   N'SQL Server'
GO

The following example creates a linked server S1_instance1 on an instance of SQL Server by using the SQL Native Client OLE DB provider (SQLNCLI).

EXEC sp_addlinkedserver   
   @server='S1_instance1', 
   @srvproduct='',
   @provider='SQLNCLI', 
   @datasrc='S1\instance1'

B. Using the Microsoft OLE DB Provider for Jet

The following example creates a linked server named SEATTLE Mktg.

Note

This example assumes that both Microsoft Access and the sample Northwind database are installed and that the Northwind database resides in C:\Msoffice\Access\Samples.

EXEC sp_addlinkedserver 
   @server = 'SEATTLE Mktg', 
   @provider = 'Microsoft.Jet.OLEDB.4.0', 
   @srvproduct = 'OLE DB Provider for Jet',
   @datasrc = 'C:\MSOffice\Access\Samples\Northwind.mdb'
GO

C. Using the Microsoft OLE DB Provider for Oracle

The following example creates a linked server named LONDON Mktg that uses the Microsoft OLE DB Provider for Oracle and assumes that the SQL*Net alias for the Oracle database is MyServer.

EXEC sp_addlinkedserver
   @server = 'LONDON Mktg',
   @srvproduct = 'Oracle',
   @provider = 'MSDAORA',
   @datasrc = 'MyServer'
GO

D. Using the Microsoft OLE DB Provider for ODBC with the data_source parameter

The following example creates a linked server named SEATTLE Payroll that uses the Microsoft OLE DB Provider for ODBC (MSDASQL) and the data_source parameter.

Note

The specified ODBC data source name must be defined as System DSN in the server before you use the linked server.

EXEC sp_addlinkedserver 
   @server = 'SEATTLE Payroll', 
   @srvproduct = '',
   @provider = 'MSDASQL', 
   @datasrc = 'LocalServer'
GO

E. Using the Microsoft OLE DB Provider for Jet on an Excel spreadsheet

To create a linked server definition using the Microsoft OLE DB Provider for Jet to access an Excel spreadsheet, first create a named range in Excel by specifying the columns and rows of the Excel worksheet to select. The name of the range can then be referenced as a table name in a distributed query.

EXEC sp_addlinkedserver 'ExcelSource',
   'Jet 4.0',
   'Microsoft.Jet.OLEDB.4.0',
   'c:\MyData\DistExcl.xls',
   NULL,
   'Excel 5.0'
GO

To access data from an Excel spreadsheet, associate a range of cells with a name. The following query can be used to access the specified named range SalesData as a table by using the linked server set up previously.

SELECT *
   FROM ExcelSource...SalesData
GO

If SQL Server is running under a domain account that has access to a remote share, a UNC path can be used instead of a mapped drive.

EXEC sp_addlinkedserver 'ExcelShare',
   'Jet 4.0',
   'Microsoft.Jet.OLEDB.4.0',
   '\\MyServer\MyShare\Spreadsheets\DistExcl.xls',
   NULL,
   'Excel 5.0'

F. Using the Microsoft OLE DB Provider for Jet to access a text file

The following example creates a linked server for directly accessing text files, without linking the files as tables in an Access .mdb file. The provider is Microsoft.Jet.OLEDB.4.0 and the provider string is Text.

The data source is the full path of the directory that contains the text files. A schema.ini file, which describes the structure of the text files, must exist in the same directory as the text files. For more information about how to create a Schema.ini file, see the Jet Database Engine documentation.

--Create a linked server.
EXEC sp_addlinkedserver txtsrv, 'Jet 4.0', 
   'Microsoft.Jet.OLEDB.4.0',
   'c:\data\distqry',
   NULL,
   'Text'
GO

--Set up login mappings.
EXEC sp_addlinkedsrvlogin txtsrv, FALSE, Admin, NULL
GO

--List the tables in the linked server.
EXEC sp_tables_ex txtsrv
GO

--Query one of the tables: file1#txt
--using a four-part name. 
SELECT * 
FROM txtsrv...[file1#txt]

G. Using the Microsoft OLE DB Provider for DB2

The following example creates a linked server named DB2 that uses the Microsoft OLE DB Provider for DB2.

EXEC sp_addlinkedserver
   @server='DB2',
   @srvproduct='Microsoft OLE DB Provider for DB2',
   @catalog='DB2',
   @provider='DB2OLEDB',
   @provstr='Initial Catalog=PUBS;
       Data Source=DB2;
       HostCCSID=1252;
       Network Address=XYZ;
       Network Port=50000;
       Package Collection=admin;
       Default Schema=admin;'

See Also

Reference

Distributed Queries Stored Procedures (Transact-SQL)
sp_addlinkedsrvlogin (Transact-SQL)
sp_addserver (Transact-SQL)
sp_dropserver (Transact-SQL)
sp_serveroption (Transact-SQL)
sp_setnetname (Transact-SQL)
System Stored Procedures (Transact-SQL)
System Tables (Transact-SQL)

Other Resources

Configuring Linked Servers for Delegation
OLE DB Providers Tested with SQL Server

Help and Information

Getting SQL Server 2005 Assistance

Change History

Release History

12 December 2006

New content:
  • Added a note to the @provstr description.