MSSqlServer remote database access

- a first portion (point)
disposed permanent link database access methods (access remote databases need regular operation) -
Exec the sp_addlinkedserver 'MyLinkServer', '', 'the SQLOLEDB', 'remote ip address or server name'
- or:
Exec the sp_addlinkedserver 'remote server', N'SQL server '
Exec sp_addlinkedsrvlogin' MyLinkServer ',' false ', null,' username ',' password '

- Returns the local server linked server defined in the list of
Exec sp_linkedservers
- return table fields, in which the MSSQL terms: SRV_NAME (name of the linked server), SRV_DATASOURCE (corresponding to the specified linked server server name)

- when accessing the table to four-part name form
- sample query
Select * From MyLinkServer name .dbo database table names.

- Import example
.. Select * Into Table From MyLinkServer database table name .dbo

- deleted will no longer use a linked server
Exec sp_dropserver 'MyLinkServer', 'droplogins ' - As a User name
: - or
when not logged on as a user name - Exec sp_dropserver 'MyLinkServer'

- Remote connection / LAN data (OpenRowSet / the OpenQuery / the OpenDataSource)
--1, OpenRowSet
- sample query
Select * From OpenRowSet ( 'SQLOLEDB' , 'SQL Server Name'; 'user name'; 'password', the database name. . the dbo table name)
- Health cost surface
Select * Into table From OpenRowSet ( 'SQLOLEDB', ' SQL server name'; 'username'; 'password', the name of the database table name .dbo).
- the introduction local table remote table
Insert OpenRowSet ( 'SQLOLEDB', ' SQL server name'; 'username';. 'password', the name of the database table name .dbo)
the Select the From local table *
- update the local table
the update B
the Set B column A =. . a column A
the From the OpenRowSet ( 'the SQLOLEDB', 'the SQL server name'; 'username'; 'password', the name of the database table name .dbo.) As a Inner Join local table B
the On a.column1 = b.column1

--OpenQuery usage need to create a connection
- first create a connection to create a linked server
Exec sp_addlinkedserver 'MyLinkServer', '' , 'SQLOLEDB', ' remote server name or IP'
- the query
Select * From OpenQuery (MyLinkServer, ' Select * . From .dbo database table name ')
- the local table into the remote table
Insert OpenQuery (MyLinkServer,' Select * From .dbo database table name ').
the Select the From local * table
- update local table
update b
. the Set column b B = a. column B
the From the OpenQuery (MyLinkServer, '* the From the Select database .dbo. table') of As A
Inner the Join local table b On a. column A = b. column A

--3, the OpenDataSource
the Select the From the OpenDataSource * ( 'the SQLOLEDB', '= the Source the Data remote server name or IP; User ID = login name; Password = password') .test.dbo.roy_ta
- the local tables into a remote table
Insert OpenDataSource ( 'SQLOLEDB', 'Data Source = remote server name or IP; User ID = login name; password = password') .dbo database table names..
the Select the From local table *


--第二部分(实际示例)
--OpenRowSet使用OLEDB的一些例子
Select * From OpenRowSet('SQLOLEDB','Server=(local);PWD=***;UID=sa;','Select * From TB.dbo.school') As t
Select * From OpenRowSet('SQLOLEDB','Server=(local);PWD=***;UID=sa;',TB.dbo.school) As t
Select * From OpenRowSet('SQLOLEDB','Server=(local);Trusted_Connection=yes;',TB.dbo.school) As t
Select * From OpenRowSet('SQLOLEDB','(local)';'sa';'***','Select * From TB.dbo.school') As t
Select * From OpenRowSet('SQLOLEDB','(local)';'sa';'***',TB.dbo.school) As t
Select * From OpenRowSet('SQLOLEDB','(local)';'sa';'***','Select school.id As id1,people.id As id2 From TB.dbo.school Inner Join TB.dbo.people On school.id=people.id') As t

--OpenRowSet使用SQLNCLI的一些例子(SQLNCLI在SqlServer2005以上才能使用)
Select * From OpenRowSet('SQLNCLI','(local)';'sa';'***','Select * From TB.dbo.school') As t
Select * From OpenRowSet('SQLNCLI','Server=(local);Trusted_Connection=yes;','Select * From TB.dbo.school') As t
Select * From OpenRowSet('SQLNCLI','Server=(local);UID=sa;PWD=***;','Select * From TB.dbo.school') As t
Select * From OpenRowSet('SQLNCLI','Server=(local);UID=sa;PWD=***;',TB.dbo.school) As t
Select * From OpenRowSet('SQLNCLI','Server=(local);UID=sa;PWD=***;DataBase=TB','Select * From dbo.school') As t

--OpenRowSet其他使用
Insert OpenRowSet('SQLNCLI','Server=(local);Trusted_Connection=yes;','Select name From TB.dbo.school Where id=1') Values('ghjkl') /*要不要Where都一样,插入一行*/
Update OpenRowSet('SQLNCLI','Server=(local);Trusted_Connection=yes;','Select name From TB.dbo.school Where id=1') Set name='kkkkkk'
Delete From OpenRowSet('SQLNCLI','Server=(local);Trusted_Connection=yes;','Select name From TB.dbo.school Where id=1')

--OpenDataSource使用SQLNCLI的一些例子
Select * From OpenDataSource('SQLNCLI','Server=(local);UID=sa;PWD=***;').TB.dbo.school As t
Select * From OpenDataSource('SQLNCLI','Server=(local);UID=sa;PWD=***;DataBase=TB').TB.dbo.school As t

--OpenDataSource使用OLEDB的例子
Select * From OpenDataSource('SQLOLEDB','Server=(local);Trusted_Connection=yes;').TB.dbo.school As t

--OpenDataSource其他使用
Insert OpenDataSource('SQLNCLI','Server=(local);Trusted_Connection=yes;').TB.dbo.school(name) Values('ghjkl') /*要不要Where都一样,插入一行*/
Update OpenDataSource('SQLNCLI','Server=(local);Trusted_Connection=yes;').TB.dbo.school Set name='kkkkkk'
Delete From OpenDataSource('SQLNCLI','Server=(local);Trusted_Connection=yes;').TB.dbo.school Where id=1

--OpenQuery使用OLEDB的一些例子
Exec sp_addlinkedserver 'MyLinkServer','','SQLOLEDB','(local)'
Exec sp_addlinkedsrvlogin 'MyLinkServer','false',null,'sa','***'
Select * From OpenQuery(MyLinkServer, 'Select *  From TB.dbo.school')

--OpenQuery使用SQLNCLI的一些例子
Exec sp_addlinkedserver 'MyLinkServerA','','SQLNCLI','(local)'
Exec sp_addlinkedsrvlogin 'MyLinkServerA','false',null,'sa','***'
Select * From OpenQuery(MyLinkServerA,'Select *  From TB.dbo.school')

Other --OpenQuery using
Insert OpenQuery (MyLinkServerA, 'Select name From TB.dbo.school Where id = 1') Values ( 'ghjkl') / * Where are the same or not to insert a row * /
the Update the OpenQuery (MyLinkServerA, 'the Select the Where the From TB.dbo.school ID = name. 1 ') the Set name =' KKKKKK '
the Delete the OpenQuery (MyLinkServerA,' name the From the Select the Where ID = TB.dbo.school. 1 ')

Guess you like

Origin www.cnblogs.com/hnllhq/p/12286909.html