OpenDataSource (TRANSACT-SQL)

Source: Internet
Author: User

Provide special connection information without using the name of the linked server and use it as part of the four-part object name.

Transact-SQL syntax conventions

Syntax

 
OpenDataSource (provider_name, init_string)

Parameters

Provider_name

Register as the name of the progid of the ole db access interface used to access the data source.Provider_nameThe data type of isChar, No default value.

Init_string

Connection string to be passed to the targetProgramOfIdatainitializeInterface. The string syntax of the provider is based on key value pairs. These key value pairs are separated by semicolons. For example:"Keyword1=Value; Keyword2=Value".

For specific keyword value pairs supported by the provider, see Microsoft Data Access SDK. This document defines the basic syntax. The following table listsInit_stringThe most common keywords in a parameter.

Keywords Ole db attributes Valid values and descriptions

Data Source

Dbprop_init_datasource

Name of the data source to be connected. Different providers use different methods to explain this. For the SQL Server native client OLE DB access interface, this indicates the name of the server. For the access interface of jet ole db, this indicates the complete path of the. MDB file or. xls file.

Location

Dbprop_init_location

The location of the database to be connected.

Extended attributes

Dbprop_init_providerstring

The connection string specified by the provider.

Connection timeout

Dbprop_init_timeout

After the timeout value is reached, the connection attempt will fail.

User ID

Dbprop_auth_userid

The User ID used for the connection.

Password

Dbprop_auth_password

The password used for the connection.

Directory

Dbprop_init_catalog

The initial or default directory name when you connect to the data source.

Integration Security

Dbprop_auth_integrated

Sspi, specifying Windows Authentication

Note

Only whenDisallowadhocaccessRegistry options are explicitly set to 0 for the specified provider and enabledAd hoc distributed queriesOpenDataSource can be used to access remote data in the ole db data source only when advanced configuration options are selected. If these options are not set, ad hoc access is not allowed by default.

The OpenDataSource function can be used in the same Transact-SQL syntax location that can use the linked server name. Therefore, OpenDataSource can be used as the first part of the four-part name, which references the name of the table or view in the select, insert, update, or delete statement; or reference the remote stored procedure in the execute statement. When executing a remote stored procedure, OpenDataSource should reference another instance of SQL Server. OpenDataSource does not accept parameter variables.

Similar to the OpenRowSet function, OpenDataSource should only reference the infrequently accessed ole db data sources. For any data sources that frequently access data, define a connection server for them. Both OpenDataSource and OpenRowset cannot provide all the functions defined by the linked server, such as security management and directory information query. All connection information (including passwords) must be provided each time OpenDataSource is called ).

Important:
Windows authentication is much safer than SQL Server Authentication. Use Windows Authentication whenever possible. OpenDataSource should not be used to connect an explicit password in a string.

The connection requirements of each provider are similar to those of the parameter requirements when a linked server is created. Detailed information about many common providers is listed in the sp_addmediaserver (TRANSACT-SQL) topic.

Permission

Any user can execute OpenDataSource. The permission used to connect to the remote server is determined by the connection string.

Example

The following example shows how to create a serverLondonSQL Server instance onPayrollAnd QueryAdventureworks. HumanResources. EmployeeTable. (Use sqlncli and the SQL server will be redirected to the latest version of the SQL Server native client OLE DB access interface .)

Select *
From OpenDataSource ('sqlncl ',
'Data source = London \ payroll; Integrated Security = sspi ')
. Adventureworks. HumanResources. Employee

The following example creates an ad hoc connection to an Excel worksheet in the format of 1997-2003.

 
Select * From OpenDataSource ('Microsoft. Jet. oledb.4.0 ',
'Data source = c: \ datafolder \ Documents \ testexcel.xls; extended properties = Excel 5.0 ')... [sheet1 $];

See

OpenRowSet (TRANSACT-SQL)
Sp_addmediaserver (TRANSACT-SQL)
Other resources

Distributed Query
Use a temporary name to identify the data source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.