sql server2005连接服务器oracle
访问接口:Microsoft OLE DB Provider for Oracle
产品名称:随便取
数据源:oracle客户端的配置,如O20002.headfree.com.cn
方法1:
SELECT * FROM OracleLinkedServerName..OwnerUserName.TableName;
链接服务器名..用户名.表名
请使用字母全部大写的名称
insert into dbo.FAC_INFO
select * from DMS..EMS.DMS_TR_DEVICE
方法2:
declare @tr_id int
set @tr_id = 287001018
SELECT * FROM OPENQUERY (DMS,'SELECT * FROM DMS_TR_DEVICE') where tr_id=@tr_id
方法3:
declare @sql nvarchar(100)
set @sql= 'SELECT * FROM OPENQUERY (DMS,''SELECT * FROM DMS_TR_DEVICE'')'
execute(@sql)
如果要保存返回结果,则可以如下:
set @sql= 'insert into ##temptable SELECT * FROM OPENQUERY (DMS,''SELECT * FROM DMS_TR_DEVICE'')'
这种方法最大的好处就是可以传递参数,而不是先查询所有记录出来再根据参数过滤
方法4:标准做法
SELECT * FROM OPENQUERY(TEST, 'SELECT * FROM tableName') where id=@Id
INSERT 示例
INSERT OPENQUERY (linked_server, 'SELECT title, content FROM msgs')
VALUES ('title', 'content');
UPDATE 示例
UPDATE OPENQUERY (linked_server, 'SELECT title, content FROM msgs WHERE id=1')
SET title = 'newTitle', content = 'newContent';
DELETE 示例
DELETE OPENQUERY (linked_server, 'SELECT id FROM msgs WHERE id=1');
注意:
OPENQUERY 不接受参数变量,也就是说:DELETE OPENQUERY (linked_server, 'SELECT id FROM msgs WHERE id=' + @id); 是不正确的,但可以这样做:DELETE OPENQUERY (linked_server, 'SELECT id FROM msgs') WHERE id=@id); 。
T-SQL 中,INSERT 可以省略 INTO,DELETE 也可以省略 FROM。
INSERT 时必须明确 SELECT 的字段。