An ad hoc name is used for infrequent queries against OLE DB data sources that are not defined as linked servers. In SQL Server, the OPENROWSET and OPENDATASOURCE functions provide connection information for accessing data from OLE DB data sources.
By default, ad hoc names are not supported. The DisallowAdhocAccess provider option must be set to 0 and the Ad Hoc Distributed Queries advanced configuration option must be enabled.
Security Note: |
|---|
|
Enabling the use of ad hoc names means that any authenticated login to SQL Server can access the provider. SQL Server administrators should enable this feature only for highly trusted providers that are safe to be accessed by any local login.
|
OPENROWSET and OPENDATASOURCE should be used only to reference OLE DB data sources that are accessed infrequently. For any data sources that will be accessed more than several times, define a linked server. Neither OPENDATASOURCE nor OPENROWSET provide all the functionality of linked server definitions. This includes security management and the ability to query catalog information. Every time that these functions are called, all connection information, including passwords, must be provided.
OPENROWSET and OPENDATASOURCE appear to be functions and for convenience are referred to as functions; however, OPENROWSET and OPENDATASOURCE are macros and do not support supplying Transact-SQL variables as arguments.
OPENROWSET can be used with any OLE DB provider that returns a rowset, and can be used anywhere a table or view reference is used in a Transact-SQL statement. OPENROWSET is specified with the following:
-
All the information required to connect to the OLE DB data source.
-
Either the name of an object that will generate a rowset, or a query that will generate a rowset.
OPENDATASOURCE provides connection information as part of a four-part object name. This function supports only OLE DB providers that expose multiple rowsets by using the catalog.schema.object notation. OPENDATASOURCE can be used in the same locations in Transact-SQL syntax that a linked server name can be used. OPENDATASOURCE is specified with the following:
-
The name registered as the PROGID of the OLE DB provider that is used to access the data source.
-
A connection string that specifies the various connection properties to be passed to the OLE DB provider. The syntax of the connection string is a sequence of keyword-value pairs. The basic syntax is defined in the Microsoft Data Access Software Development Kit, and each provider documents the specific keyword-value pairs it supports. For more information about connection strings, see OPENDATASOURCE (Transact-SQL).
For more information, see Identifying a Data Source by Using an Ad Hoc Name.