SQL Server Access database across servers (openrowset/opendatasource/openquery)

Source: Internet
Author: User
Tags sql server books

SQL Server Access database across servers (openrowset/opendatasource/openquery)

1. Enable AD Hoc distributed Queries

Before using Openrowset/opendatasource to enable the ad Hoc distributed queries service, because this service is not secure, SQL Server is turned off by default

Methods for enabling AD HOC distributed queries

SQL Server blocked STATEMENT ' openrowset/opendatasource ' for component ' Ad Hoc distributed Queries '

Access, because this component has been shut down as part of this server's security configuration. System administrators can use the

sp_configure enable ' Ad Hoc distributed Queries '. For more information about enabling ' Ad Hoc distributed Queries '

Information, see "Surface area Configurator" in SQL Server Books Online.
To enable the ad Hoc distributed queries method, execute the following query statement:

exec sp_configure ' show advanced options ', 1
Reconfigure
exec sp_configure ' Ad Hoc distributed Queries ', 1
Reconfigure
After use, remember to close it, because this is a security risk, remember to execute the following SQL statement

exec sp_configure ' Ad Hoc distributed Queries ', 0
Reconfigure
exec sp_configure ' show advanced options ', 0
Reconfigure

2. Examples of Use

--Create a linked server
Execsp_addlinkedserver‘Itsv‘,‘‘,‘SQLOLEDB‘,‘Remote server name or IP address‘
Execsp_addlinkedsrvlogin‘Itsv‘,‘False‘,Null,‘User name‘,‘Password‘

--query Example
Select*FromITSV. Database name. dbo. Table name

--Import sample
Select*IntoTableFromITSV. Database name. dbo. Table name

--Remove linked server when no longer in use
ExecSp_dropserver‘Itsv‘,‘Droplogins‘

--Connecting Remote/LAN data (Openrowset/openquery/opendatasource)
--1, OpenRowset

--query Example
Select*FromOpenRowset(‘SQLOLEDB‘,‘SQL Server Name‘;‘User name‘;‘Password‘, database name. dbo. Table name)

--Raw cost surface
Select*IntoTableFromOpenRowset(‘SQLOLEDB‘,‘SQL Server Name‘;‘User name‘;‘Password‘, database name. dbo. Table name)

--Importing a local table to a remote table
InsertOpenRowset(‘SQLOLEDB‘,‘SQL Server Name‘;‘User name‘;‘Password‘, database name. dbo. Table name)
Select*FromLocal surface

--Update local surface
UpdateB
SetB. Column A=A. Column A
FromOpenRowset(‘SQLOLEDB‘,‘SQL Server Name‘;‘User name‘;‘Password‘, database name. dbo. Table name)AsAInnerJoinLocal surface B
OnA.column1=B.column1

--OPENQUERY usage requires creating a connection

--Create a linked server by creating a connection first
Execsp_addlinkedserver‘Itsv‘,‘‘,‘SQLOLEDB‘,‘Remote server name or IP address‘
--Inquire
Select*
FromOpenQuery(ITSV,‘Select * from database. dbo. Table name‘)
--Importing a local table to a remote table
InsertOpenQuery(ITSV,‘Select * from database. dbo. Table name‘)
Select*FromLocal surface
--Update local surface
UpdateB
SetB. Column B=A. Column B
FromOpenQuery(ITSV,‘Select * from database. dbo. Table name‘)AsA
InnerJoinLocal surface BOnA. Column A=B. Column A

--3, Opendatasource/openrowset
Select*
FromOpenDataSource(‘SQLOLEDB‘,‘Data Source=ip/servername; User id= login name; password= Password‘). Test.dbo.roy_ta
-- Import the local table into the remote table   insert  Opendatasource ( sqloledb  ' data source=ip/servername; user id= login name; password= password   "). database. dbo. Table name  
select *  from  local table

SQL Server Access database across servers (openrowset/opendatasource/openquery)

Related Article

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.