--远程链接服务器机器名:Roy;实例名:Roy/SQL2005DE;登录名:sa;密码:Test2005;
--創建链接服务器
EXEC master.dbo.sp_addlinkedserver @server = N'testLink', @srvproduct=N'databasename', @provider=N'SQLOLEDB', @datasrc=N'192.168.1.12'--SQL05可用SQLNCLI/SQLOLEDB
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'testLink', @locallogin = NULL , @useself = N'False', @rmtuser = N'sa', @rmtpassword = N'cxxch@ere_rta'
GO
--刪除链接服务器
exec sp_dropserver 'testLink','droplogins'
--創建链接服务器
EXEC master.dbo.sp_addlinkedserver @server = N'testLink', @srvproduct=N'databasename', @provider=N'SQLOLEDB', @datasrc=N'192.168.1.12'--SQL05可用SQLNCLI/SQLOLEDB
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'testLink', @locallogin = NULL , @useself = N'False', @rmtuser = N'sa', @rmtpassword = N'cxxch@ere_rta'
GO
--刪除链接服务器
exec sp_dropserver 'testLink','droplogins'
用openrowset:
--支持存儲過程
select *from openrowset('SQLOLEDB','Roy/SQL2005DE';'sa';'Test2005','test.dbo.p')a
--select
select *from openrowset('SQLOLEDB','Roy/SQL2005DE';'sa';'Test2005',test.dbo.Tab)a
--insert
insert openrowset('SQLOLEDB','Roy/SQL2005DE';'sa';'Test2005',test.dbo.Tab)(Name)values('EE')
--update
update openrowset('SQLOLEDB','Roy/SQL2005DE';'sa';'Test2005',test.dbo.Tab) set Name='EEE' where Name=N'EE'
--delete
delete openrowset('SQLOLEDB','Roy/SQL2005DE';'sa';'Test2005',test.dbo.Tab) where Name=N'EEE'
用openquery:--支持存儲過程
--支持存儲過程
select * from openquery(roy_lnk, 'test.dbo.P')
--select
SELECT * FROM openquery(roy_lnk, 'SELECT * FROM test.dbo.tab')
--insert
insert 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=Roy/SQL2005DE;User ID=sa;Password=Test2005').'test.dbo.p'
--select
select * from OPENDATASOURCE('SQLOLEDB','Data Source=Roy/SQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab
--insert
insert OPENDATASOURCE('SQLOLEDB','Data Source=Roy/SQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab(Name) values('H')
--update
update OPENDATASOURCE('SQLOLEDB','Data Source=Roy/SQL2005DE;User ID=sa;Password=Test2005').test.dbo.tab set Name='HH' where Name='H'
--delete
delete OPENDATASOURCE('SQLOLEDB','Data Source=Roy/SQL2005DE;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