SQL SERVER 2008查询其他数据库

1、访问本地的其他数据库

--启用Ad Hoc Distributed Queries--

exec sp_configure 'show advanced options',1

reconfigure



exec sp_configure 'Ad Hoc Distributed Queries',1

reconfigure



-- 使用完成后,关闭Ad Hoc Distributed Queries--

exec sp_configure 'Ad Hoc Distributed Queries',0

reconfigure



exec sp_configure 'show advanced options',0

reconfigure



--SELECT * FROM   opendatasource('SQLOLEDB ','Data Source=ip(或ServerName);User ID=登陆名;Password=密码 ').数据库.dbo.表名(或视图)



SELECT * FROM 

         opendatasource(

          'SQLOLEDB ',

            'Data Source=ITVS;

              User ID=sa;Password=sa')

               .ANSVSP.dbo.serv

 

2、访问其他机器上的数据库

 (1)将远程机器上的sql server远程打开

 (2)将远程机器上的防火墙关掉

SELECT * FROM 

         opendatasource(

          'SQLOLEDB',

            'Data Source=192.168.1.26;

              User ID=sa;Password=sadan ')

               .ANSVSP.dbo.serv

 内联

SELECT a.bah,a.mz,b.* FROM 

         opendatasource(

          'SQLOLEDB',

            'Data Source=192.168.1.26;

              User ID=sa;Password=sadan ')

               .ANSVSP.dbo.serv as a inner join db_local_table b on a.bah=b.id collate Chinese_PRC_90_CI_AI;



--collate Chinese_PRC_90_CI_AI 保持等号两边的排序规则一致即可

 修改

UPDATE b SET b.fhsj=a.date_end FROM 

         opendatasource(

          'SQLOLEDB',

            'Data Source=192.168.1.26;

              User ID=sa;Password=sadan')

               .ANSVSP.[dbo].[View_Funeral_TR] as a inner join hzxxb b on a.id=b.bah collate Chinese_PRC_90_CI_AI WHERE fhsj is null;

 

你可能感兴趣的:(SQL Server 2008)