Operation between different SQL Server databases on different servers

What is cross-server operations?

Cross-server operations that can be connected to a local database on a remote server, you can perform database operations on the other side of the database, such as CRUD.

Why cross-server operations

With the increasing amount of data, the volume of business expansion, needs to be installed on different servers in different databases, sometimes because the business needs, the data from different servers to integrate, this time on the need for a cross-server operations.

What tools are cross-server operations?

DBLINK (database link), the name suggests is a link database, just like a telephone line, is a channel, when we have to cross a local database, access data in the table to another database, local database, it is necessary to create a remote database dblink You can access data in a remote database tables as access to local databases as through a local database dblink.

method one:

Create a SQL Server remote link:

1. Log on to the local database -> Server Object -> Linked Server (right) -> New Linked Server

2. In the dialog box, enter the relevant information

3. Click on the left side of "security", the following page, enter the account password to other databases

4. Click "OK" after successfully created, the following chart you can see the created linked server

5. Use the following link to try to create a good look-up table of the other server to verify

 Original Address: http://baijiahao.baidu.com/s?id=1601340639397887562&wfr=spider&for=pc

 

 

Second way:

The same server different databases

the SELECT  *  from the database name .. table name or the SELECT  *  from the database name .dbo. table

1. Create a linked server

1.1 Creating a link name

Exec the sp_addlinkedserver ' LinkName ' , '' , ' the SQLOLEDB ' , ' remote server name or ip address '    - you have a custom instance name plus "/ examples of" 

 / * For example: exec sp_addlinkedserver 'TonyLink', ' ', 'the SQLOLEDB', '192.168.2.110' * /  

1.2 create a login (or create linked server login name called mapping) (Just choose one way)

1.2.1 Log in to windows authentication mode

exec sp_addlinkedsrvlogin 'LinkName'  --或exec sp_addlinkedsrvlogin 'LinkName', 'true'

 /*例如:exec sp_addlinkedsrvlogin 'TonyLink'  */  

1.2.2 SQL authentication to log on                                                                    

Exec the sp_addlinkedsrvlogin ' LinkName is ' , ' to false ' , NULL , ' username ' , ' password ' 

/ * For example: the sp_addlinkedsrvlogin Exec 'TonyLink', 'to false', null, 'SA', '123456' * /

 

2. linked server data operations

2.1 Queries

the SELECT  *  from LinkName database schema name name name of the table... 

/ * For example: the SELECT * from TonyLink.LoadData.dbo.XimaArea * /

Data 2.2 remote database queries into new local table (table need not be present locally in advance, it will be created automatically)

the SELECT   *  INTO table name from ... LinkName database table schema name name name 

/ * For example: select * into Newtb fromTonyLink.LoadData.dbo.XimaArea * /

2.3 update

Update ... LinkName is the name of the database schema name table set field = ' value '  WHERE field = ' condition ' 

/ * For example: update TonyLink.LoadData.dbo.XimaArea set area = 'China region' where ArealD. 8 = * /

2.4 Delete

Delete ... LinkName is the name of the database schema name table where field name = ' condition ' 

/ * For example: where ArealD = Delete TonyLink.LoadData.dbo.XimaArea. 1 * /

Delete link server when no longer in use

exec sp_dropserver ' Link Name ' , ' droplogins '

3. By rowset functions (openquery / openrowset / opendatasource) Operation

3.1 openquery method (needs the linked server you just created):

3.1.1 Queries

SELECT  *  from  OPENQUERY (linkname, ' SELECT * from database schema name Name Table. ' ) 

/ * For example: SELECT * from OPENQUERY (TonyLink, 'SELECT * from LoadData.dbo.XimaArea') * /

3.1.2 Import

3.1.2.1 The local table (local table requires the presence of advance) into a remote table (correspondence between the two columns to the table)
INSERT  OPENQUERY (linkname, ' SELECT * from database schema name Name Table.. ' ) SELECT  *  from the local table 

/ * For example: INSERT OPENQUERY (TonyLink, 'Area from LoadData.dbo.XimaArea SELECT') from TTT Area SELECT * /
3.1.2.2 The local table (local table requires the presence of advance) introducing the specified remote table column (column for a correspondence between the two tables)
I nsert OPENQUERY (linkname, ' .. * SELECT schema name from the name of the database table name ' ) (column, row ...) SELECT columns, column ... from the local table / * For example: insert openquery (TonyLink, 'select * LoadData.dbo.ximajxs from ') (jxsName, Consignee, address) 
    SELECT jxsName, Consignee, from TTT address   * / 




3.1.3 update

update openquery(linkname, 'select * from 数据库名.架构名.表名') set 字段='' where 字段='条件'

/*例如:update openquery(TonyLink, 'select * from LoadData.dbo.ximajxs') set JxsName='北京有限公司' where jxsId=10 */

3.1.4 删除

delete openquery(linkname, 'select * from 数据库名.架构名.表名') where 字段名='条件'

/*例如:delete openquery(TonyLink, 'select * from LoadData.dbo.ximajxs') where jxsId=10 */

3.2 openrowset:

3.2.1 查询

select * from openrowset('SQLOLEDB', 'SQL服务器名'; '用户名'; '密码', 数据库名.dbo.表名)

报错问题:

解决方法:

在数据库服务实例名(如图1-3,GP-PC\sql2008位置)点击鼠标右键【方面】,在窗口【查看方面】— 点击【常规】— 【方面】— 选择【外围应用配置器】,找到【AdHocRemoteQueriesEnabled】— 选择【True】— 点击【确定】

3.2.2 生成本地表

select * into 表名 from openrowset('SQLOLEDB', 'SQL服务器名'; '用户名'; '密码', 数据库名.dbo.表名)

3.2.3 把本地表导入到远程表

insert openrowset('SQLOLEDB', 'SQL服务器名'; '用户名'; '密码', 数据库名.dbo.表名)

select * from 本地表

3.2.4 更新本地表

update b
set b.address = a.area
from openrowset('SQLOLEDB', '192.168.2.110'; 'sa'; '123456', LoadData.dbo.XimaArea) as a
inner join ttt b
on a.areaid=b.areaid

3.3 opendatasource

3.3.1  查询

select * from opendatasource('SQLOLEDB', 'Data Source=192.168.2.110; uid=sa; pwd=123456').LoadData.dbo.XimaArea

 4. 具体例子

if exists(select 1 from master.dbo.sysservers where srvname='linktest')
begin
    exec sys.sp_droplinkedsrvlogin 'linktest', 'sa'
    exec sys.sp_dropserver 'linktest'
end

exec sys.sp_addlinkedserver
    @server = 'linktest', -- sysname
    @srvproduct = N'', -- nvarchar(128)
    @provider = N'SQLOLEDB', -- nvarchar(128)
    @datasrc = N'192.168.2.110' -- nvarchar(4000)

exec sys.sp_addlinkedsrvlogin 
    @rmtsrvname = 'linktest', -- sysname
    @useself = 'false', -- varchar(8)
    @locallogin = null, -- sysname
    @rmtuser = 'sa', -- sysname
    @rmtpassword = '123456' -- sysname

select * from linktest.LoadData.dbo.ximalss

if exists(select 1 from master.dbo.sysservers where srvname='linktest')
begin
    exec sys.sp_droplinkedsrvlogin 'linktest', 'sa'
    exec sys.sp_dropserver 'linktest'
end

go

参考链接:https://www.cnblogs.com/w-y-f/archive/2012/05/07/2488474.html

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhaoyl9/p/11527090.html