Вы находитесь на странице: 1из 4

OPENDATASOURCE (Transact-SQL)

SQL Server 2012 3 out of 13 rated this helpful Provides ad hoc connection information as part of a four-part object name without using a linked server name. Transact-SQL Syntax Conventions

Syntax

O P E N D A T A S O U R C E(p r o v i d e r _ n a m e ,i n i t _ s t r i n g)

Arguments
provider_name Is the name registered as the PROGID of the OLE DB provider used to access the data source. provider_name is a char data type, with no default value. init_string Is the connection string passed to the IDataInitialize interface of the destination provider. The provider string syntax is based on keywordvalue pairs separated by semicolons, such as: 'keyword1=value; keyword2=value'. For specific keyword-value pairs supported on the provider, see the Microsoft Data Access SDK. This documentation defines the basic syntax. The following table lists the most frequently used keywords in the init_string argument. Keyword Data Source OLE DB property DBPROP_INIT_DATASOURCE Valid values and description Name of the data source to connect to. Different providers interpret this in different ways. For SQL Server Native Client OLE DB provider, this indicates the name of the server. For Jet OLE DB provider, this indicates the full path of the .mdb file or .xls file.

Location Extended Properties Connect timeout User ID Password Catalog Integrated Security

DBPROP_INIT_LOCATION DBPROP_INIT_PROVIDERSTRING

Location of the database to connect to. The provider-specific connect-string.

DBPROP_INIT_TIMEOUT

Time-out value after which the connection try fails.

DBPROP_AUTH_USERID DBPROP_AUTH_PASSWORD DBPROP_INIT_CATALOG DBPROP_AUTH_INTEGRATED

User ID to be used for the connection. Password to be used for the connection. The name of the initial or default catalog when connecting to the data source. SSPI, to specify Windows Authentication

Remarks
OPENDATASOURCE can be used to access remote data from OLE DB data sources only when the DisallowAdhocAccess registry option is explicitly set to 0 for the specified provider, and the Ad Hoc Distributed Queries advanced configuration option is enabled. When these options are not set, the default behavior does not allow for ad hoc access. The OPENDATASOURCE function can be used in the same Transact-SQL syntax locations as a linked-server name. Therefore, OPENDATASOURCE can be used as the first part of a four-part name that refers to a table or view name in a SELECT, INSERT, UPDATE, or DELETE statement, or to a remote stored procedure in an EXECUTE statement. When executing remote stored procedures, OPENDATASOURCE should refer to another instance of SQL Server. OPENDATASOURCE does not accept variables for its arguments. Like the OPENROWSET function, OPENDATASOURCE should only reference OLE DB data sources that are accessed infrequently. Define a linked server for any data sources accessed more than several times. Neither OPENDATASOURCE nor OPENROWSET provide all the functionality of linked-server definitions, such as security management and the ability to query catalog information. All connection information, including passwords, must be provided every time that OPENDATASOURCE is called. Important

Important Windows Authentication is much more secure than SQL Server Authentication. You should use Windows Authentication whenever possible. OPENDATASOURCE should not be used with explicit passwords in the connection string. The connection requirements for each provider are similar to the requirements for those parameters when creating linked servers. The details for many common providers are listed in the topic sp_addlinkedserver (Transact-SQL). Any call to OPENDATASOURCE, OPENQUERY, or OPENROWSET in the FROM clause is evaluated separately and independently from any call to these functions used as the target of the update, even if identical arguments are supplied to the two calls. In particular, filter or join conditions applied on the result of one of those calls have no effect on the results of the other.

Permissions
Any user can execute OPENDATASOURCE. The permissions that are used to connect to the remote server are determined from the connection string.

Examples
The following example creates an ad hoc connection to the P a y r o l linstance of SQL Server on server L o n d o n , and queries the A d v e n t u r e W o r k s 2 0 1 2 . H u m a n R e s o u r c e s . E m p l o y e etable. (Use SQLNCLI and SQL Server will redirect to the latest version of SQL Server Native Client OLE DB Provider.)

S E L E C T* F R O MO P E N D A T A S O U R C E ( ' S Q L N C L I ' , ' D a t aS o u r c e = L o n d o n \ P a y r o l l ; I n t e g r a t e dS e c u r i t y = S S P I ' ) . A d v e n t u r e W o r k s 2 0 1 2 . H u m a n R e s o u r c e s . E m p l o y e e The following example creates an ad hoc connection to an Excel spreadsheet in the 1997 - 2003 format.

S E L E C T*F R O MO P E N D A T A S O U R C E ( ' M i c r o s o f t . J e t . O L E D B . 4 . 0 ' , ' D a t aS o u r c e = C : \ D a t a F o l d e r \ D o c u m e n t s \ T e s t E x c e l . x l s ; E x t e n d e dP r o p e r t i e s = E X C E L5 . 0 ' ) . . . [ S h e e t 1 $ ];

' D a t aS o u r c e = C : \ D a t a F o l d e r \ D o c u m e n t s \ T e s t E x c e l . x l s ; E x t e n d e dP r o p e r t i e s = E X C E L5 . 0 ' ) . . . [ S h e e t 1 $ ];

See Also
Reference
OPENROWSET (Transact-SQL) sp_addlinkedserver (Transact-SQL)

Community Additions
2013 Microsoft. All rights reserved.

Вам также может понравиться