OpenQuery:
用途:与其他Server交互的技术,通过它能够直接访问其他数据库资源。可以跨平台连接,包括Oracle
--创建链接服务器
exec sp_addlinkedserver ‘ITSV‘ , ‘‘ , ‘SQLOLEDB‘ , ‘远程服务器名或ip地址‘
exec sp_addlinkedsrvlogin ‘ITSV‘ , ‘false‘ , null , ‘用户名‘ , ‘密码
写法:
(1)查询
select * from OpenQuery([linkerServerName],‘Select * from t1 where id <10‘)
带参数查询:
--限定条件在外面 缺点数据量大效率略低
select * from OPENQUERY (linked_server, ‘SELECT id FROM msgs‘) WHERE [email protected])
--拼接字符串传参
DECLARE @Sql VARCHAR(1000)
set @sqlstr = ‘select * from openquery(ikangmis,select * from table where id=‘‘‘[email protected]+‘‘‘)‘;
exec ( @sqlstr );
EXEC(@Sql)
(2)插入
insert into openquery(linkservername,‘select * from table1‘) select * from table2 where id=2
原文地址:https://www.cnblogs.com/ywkcode/p/10886365.html
时间: 2024-10-07 01:30:23