SQLServer linked server using remote queries

- Create a linked server   
Exec the sp_addlinkedserver    ' ITSV ' , '  ' , ' the SQLOLEDB ' , ' remote server name or ip address '   
Exec sp_addlinkedsrvlogin ' ITSV ' , ' false ' , null , ' username ' , ' password '  
 
- delete link server   
Exec sp_droplinkedsrvlogin ' ITSV ' , null 
Exec sp_dropserver   ' ITSV '

--Sample queries  
 the SELECT * from ITSV name .dbo database table names.  
   
 - Import sample  
 the SELECT * INTO table from . ITSV name .dbo database table names  
   
 - Remove Link server will no longer use the   
Exec sp_dropserver   ' ITSV ' , ' droplogins '  
    
- remote connection / LAN data (OPENROWSET / OPENQUERY / OPENDATASOURCE)  
 - . 1 , OPENROWSET  
   
 - query example  
 SELECT * from OPENROWSET ( ' the SQLOLEDB ' , ' SQL server name ' ; ' username ' ;' Password ' ., .Dbo database table name)  
   
 - Health Cost Surface  
 SELECT * INTO table from OPENROWSET ( ' the SQLOLEDB ' , ' SQL server name ' ; ' username ' ; ' password ' , .dbo database table name. )  
   
 - the local tables into a remote table   
INSERT OPENROWSET ( ' the SQLOLEDB ' , ' SQL server name ' ; ' username ' ; ' password ' , database name .dbo.  
Table name) the SELECT * from local tables  
   
- update the local table   
Update B   
SET B column = A. A column of A.  
 From OPENROWSET ( ' the SQLOLEDB ' , ' SQL server name ' ; ' username ' ; ' password ' , the name of the database table name .dbo.) AS A Inner join local table b   
ON a.column1 = b.column1  
   
 - OPENQUERY usage need to create a connection  
   
 - first create a connection to create a linked server   
Exec the sp_addlinkedserver    ' ITSV ' , '  ' , ' the SQLOLEDB ' , 'Remote server name or ip address '   
- Query  
 the SELECT *   
the FROM OPENQUERY (ITSV,   ' the SELECT * .dbo the FROM database table name. ' )  
 - the local table into the remote table   
INSERT OPENQUERY (ITSV,   ' the SELECT * the FROM database .dbo. table name ' )  
 SELECT * from the local table  
 - update the local table   
update b   
SET . column b = B A column B.   
the fROM OPENQUERY (ITSV,   ' the SELECT * .dbo the fROM database table name. ' ) AS A    
Inner the Join local table b . on a column A = B column A.  
   
 - . 3 , OPENDATASOURCE /openrowset  
SELECT   *  
FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta 

 

Guess you like

Origin www.cnblogs.com/Summer6/p/11129344.html