请参考:
远程连接操作
/**************************************************Tab表:/ID 自增列 Name----------- ---------1 a2 b3 c/存储过程:/create proc Passelect top 10 ID,Name from sysobjects/ ****************************************** --远程链接服务器机器名:Roy;实例名:RoySQL2005DE;登陆名:sa;密码:Test2005; --创建链接服务器EXEC master.dbo.sp_addlinkedserver @server = N'ROY_LNK', @srvproduct=N'ROY_LNK', @provider=N'SQLOLEDB', @datasrc=N'RoySQL2005DE'--SQL05可用SQLNCLISQLOLEDBGOEXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'ROY_LNK', @locallogin = NULL , @useself = N'False', @rmtuser = N'sa', @rmtpassword = N'Test2005'GO --删除链接服务器--exec sp_dropserver 'roy_lnk','droplogins' --Selectselect from roy_lnk.test.dbo.Tab --insertinsert roy_lnk.test.dbo.Tab(Name)--指定列Values('d') --updateupdate roy_lnk.test.dbo.Tab set Name='DD' where Name='d' --delete delete roy_lnk.test.dbo.Tab where Name='DD' 用openrowset: --支持存储过程select from openrowset('SQLOLEDB','RoySQL2005DE';'sa';'Test2005','test.dbo.p')a --select select from openrowset('SQLOLEDB','RoySQL2005DE';'sa';'Test2005',test.dbo.Tab)a --insertinsert openrowset('SQLOLEDB','RoySQL2005DE';'sa';'Test2005',test.dbo.Tab)(Name)values('EE') --update update openrowset('SQLOLEDB','RoySQL2005DE';'sa';'Test2005',test.dbo.Tab) set Name='EEE' where Name=N'EE' --deletedelete openrowset('SQLOLEDB','RoySQL2005DE';'sa';'Test2005',test.dbo.Tab) where Name=N'EEE' 用openquery:--支持存储过程 --支持存储过程select from openquery(roy_lnk, 'test.dbo.P') --selectSELECT FROM openquery(roy_lnk, 'SELECT FROM test.dbo.tab') --insertinsert openquery(roy_lnk, 'SELECT FROM test.dbo.tab') (Name) values('F') --update update openquery(roy_lnk, 'SELECT FROM test.dbo.tab') set Name='FF' where Name='F' --delete不支持是根据 OLE DB 提供者的功能而定--delete openquery(roy_lnk, 'SELECT FROM test.dbo.tab') where Name='FF' 用OPENDATASOURCE: --支持存储过程select from OPENDATASOURCE('SQLOLEDB','Data Source=RoySQL2005DE;User ID=sa;Password=Test2005').'test.dbo.p' --selectselect * from OPENDATASOURCE('SQLOLEDB','Data Source=RoySQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab --insertinsert OPENDATASOURCE('SQLOLEDB','Data Source=RoySQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab(Name) values('H') --updateupdate OPENDATASOURCE('SQLOLEDB','Data Source=RoySQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab set Name='HH' where Name='H' --delete delete OPENDATASOURCE('SQLOLEDB','Data Source=RoySQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab where Name='HH' --与本机表数据对比时用别名
select * from roy_lnk.test.dbo.Tab a join tab b on a.ID=b.ID