SQL cross-database data synchronization, cross-database updates across the data

 

 

Copy the code
  1 - Create a linked server 
  2 
  3 Exec the sp_addlinkedserver 'ITSV', '', 'the SQLOLEDB', 'remote server name or ip address' 
  4 
  5 Exec sp_addlinkedsrvlogin' ITSV ',' false ', null,' username ',' password ' 
  . 6 
  . 7   
  . 8 
  . 9 - query example 
 10 
 . 11 SELECT * from ITSV .dbo database table name. 
 12 is 
 13 is   
 14 
 15 - import example 
 16 
 ... 17 SELECT * from ITSV INTO table name .dbo database table 
 18 is 
 . 19   
 20 is 
 21 - delete link server will no longer use 
 22 
 23 Exec sp_dropserver 'ITSV', 'droplogins'
 24 
 25   
 26 is 
 27 - Connection Remote / LAN data (OPENROWSET / OPENQUERY / OPENDATASOURCE) 
 28
 --1 29, OPENROWSET 
 30 
 31 is   
 32 
 33 is - query example 
 34 is 
 35 SELECT * from OPENROWSET ( 'the SQLOLEDB', 'SQL server name'; 'username'; 'password', .dbo database table name.) 
 36 
 37 [   
 38 is 
 39 - the Sheng - cost surface 
 40 
 41 is SELECT * INTO table from openrowset (. 'SQLOLEDB', 'sql server name'; 'username'; 'password', the name of the database table name .dbo) 
 42 is 
 43 is   
 44 is 
 45 - the local table into the remote table 
 46 is 
 47 INSERT OPENROWSET ( 'the SQLOLEDB', 'SQL server name'; 'username'; 'password' database name .dbo.Table name) 
 48 
 49 SELECT * from the local table 
 50 
 51 is   
 52 is 
 53 is - Update the local table 
 54 is 
 55 B Update 
 56 is 
 . 57 is A = a SET column B column A. 
 58 
 59 from OPENROWSET ( 'the SQLOLEDB', 'SQL server name'; 'username';. 'Password', .dbo database table name) as a inner join local table B 
 60 
 61 is ON a.column1 = b.column1 
 62 is 
 63 is   
 64 
 65 use --openquery need to create a connection 
 66 
 67   
 68 
 69 - creating a first link connected to the server to create 
 70 
 71 is the sp_addlinkedserver Exec 'ITSV', '', 'the SQLOLEDB', 'remote server name or ip address' 
 72 
 73 is - - query 
 74 
 75 SELECT * 
 76 
 77 the FROM OPENQUERY (ITSV, 'the SELECT * .dbo the FROM database table name.') 
 78 
 79 - the local tables into a remote table 
 80 
 81 INSERT OPENQUERY (ITSV, 'the SELECT * .dbo the FROM database. Table Name ') 
 82 
 83 SELECT * from the local table 
 84
 85 - update the local table
 86 
 87 Update B 
 88 
 89 B SET. Column B = a. Column B 
 90 
 91 is the FROM OPENQUERY (ITSV, 'the SELECT * the FROM database .dbo. Table') AS A   
 92 
 93 Inner the Join local table b on a. Column A = . b column A 
 94 
 95   
 96 
 97 --3, OPENDATASOURCE / OPENROWSET 
 98 
 99 the SELECT * 
100 
101 the FROM OPENDATASOURCE ( 'the SQLOLEDB', 'the Data = the Source IP / ServerName; login name of the User ID =; password = password') .test. dbo.roy_ta 
102 
103 - the local tables into a remote table 
104 
105 INSERT OPENDATASOURCE ( 'the SQLOLEDB', 'the Data = the Source IP / ServerName; login name of the User ID =; password = password') .dbo database table name. 
106 
107 select * from
Copy the code

 If the error when performed on the implementation of the following two;

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

Cross-table update table data

Copy the code
exec sp_addlinkedserver 'ITSV', '' , 'SQLOLEDB', 'SERVER \ SQLEXPRESS' - create a stored procedure remote database connection 

--exec sp_dropserver 'ITSV', 'droplogins ' - the line does not delete the stored procedure 
 
- Update local table 
Update B 
SET b.Levels = a.Levels, b.Plogons = a.Plogons 
the FROM OPENQUERY (ITSV, 'the FROM LandSection.dbo.LandLevel the SELECT *') AS A   
Inner the Join [the dbo]. [landlevel] ON A B .ID = b.ID
Copy the code
 
 
 
 
 
If there is execution error Ad Hoc Distributed Queries: 

启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

 

Copy the code
  1 - Create a linked server 
  2 
  3 Exec the sp_addlinkedserver 'ITSV', '', 'the SQLOLEDB', 'remote server name or ip address' 
  4 
  5 Exec sp_addlinkedsrvlogin' ITSV ',' false ', null,' username ',' password ' 
  . 6 
  . 7   
  . 8 
  . 9 - query example 
 10 
 . 11 SELECT * from ITSV .dbo database table name. 
 12 is 
 13 is   
 14 
 15 - import example 
 16 
 ... 17 SELECT * from ITSV INTO table name .dbo database table 
 18 is 
 . 19   
 20 is 
 21 - delete link server will no longer use 
 22 
 23 Exec sp_dropserver 'ITSV', 'droplogins'
 24 
 25   
 26 is 
 27 - Connection Remote / LAN data (OPENROWSET / OPENQUERY / OPENDATASOURCE) 
 28
 --1 29, OPENROWSET 
 30 
 31 is   
 32 
 33 is - query example 
 34 is 
 35 SELECT * from OPENROWSET ( 'the SQLOLEDB', 'SQL server name'; 'username'; 'password', .dbo database table name.) 
 36 
 37 [   
 38 is 
 39 - the Sheng - cost surface 
 40 
 41 is SELECT * INTO table from openrowset (. 'SQLOLEDB', 'sql server name'; 'username'; 'password', the name of the database table name .dbo) 
 42 is 
 43 is   
 44 is 
 45 - the local table into the remote table 
 46 is 
 47 INSERT OPENROWSET ( 'the SQLOLEDB', 'SQL server name'; 'username'; 'password' database name .dbo.Table name) 
 48 
 49 SELECT * from the local table 
 50 
 51 is   
 52 is 
 53 is - Update the local table 
 54 is 
 55 B Update 
 56 is 
 . 57 is A = a SET column B column A.
 58 
 84
 59 from openrowset ( 'SQLOLEDB', 'sql server name'; 'username';. 'Password', .dbo database table name) as a inner join local table B 
 60 
 61 is ON a.column1 b.column1 = 
 62 is 
 63 is   
 64 
 65 use --openquery need to create a connection 
 66 
 67   
 68 
 69 - creating a first link connected to the server to create 
 70 
 71 is the sp_addlinkedserver Exec 'ITSV', '', 'the SQLOLEDB', 'remote server name or ip address' 
 72 
 73 is - query 
 74 
 75 SELECT * 
 76 
 77 the FROM OPENQUERY (ITSV, 'the SELECT * .dbo the FROM database table name.') 
 78 
 79 - the local tables into a remote table 
 80 
 . 81 INSERT OPENQUERY (ITSV, 'the SELECT * the FROM database table .dbo name ') 
 82 
 83 SELECT * from the local table
 85 - update the local table 
 86 
 87 Update b 
 88 
 .. 89 SET B = a b row column B 
 90 
 91 is the FROM OPENQUERY (ITSV, '. .Dbo the SELECT * the FROM database table name') AS A   
 92 
 93 Inner the Join local table b column A = b on a column of A.. 
 94 
 95   
 96 
 97 --3, OPENDATASOURCE / OPENROWSET 
 98 
 99 the SELECT * 
100 
101 the FROM OPENDATASOURCE ( 'the SQLOLEDB', 'the Data = the Source IP / ServerName; login name of the User ID =; Password = password ') .test.dbo.roy_ta 
102 
103 - the local tables into a remote table 
104 
105 INSERT OPENDATASOURCE (' the SQLOLEDB ',' the Data = the Source IP / ServerName; login name of the User ID =; password = password ') database. dbo. table name 
106 
107 SELECT * from
Copy the code

 If the error when performed on the implementation of the following two;

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

Cross-table update table data

Copy the code
exec sp_addlinkedserver 'ITSV', '' , 'SQLOLEDB', 'SERVER \ SQLEXPRESS' - create a stored procedure remote database connection 

--exec sp_dropserver 'ITSV', 'droplogins ' - the line does not delete the stored procedure 
 
- Update local table 
Update B 
SET b.Levels = a.Levels, b.Plogons = a.Plogons 
the FROM OPENQUERY (ITSV, 'the FROM LandSection.dbo.LandLevel the SELECT *') AS A   
Inner the Join [the dbo]. [landlevel] ON A B .ID = b.ID
Copy the code
 
 
 
 
 
If there is execution error Ad Hoc Distributed Queries: 

启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

Guess you like

Origin www.cnblogs.com/kelly1314/p/11454250.html