zoukankan      html  css  js  c++  java
  • sql跨库查询

    方式一:

    使用连接服务器

    sp_addlinkedserver 'mylink','','SQLOLEDB','(local)'
    sp_addlinkedsrvlogin 'mylink','false',null,'sa','sa'
    select * from [mylink].test.dbo.Student

    或者在 对象资源管理器-服务器对象-连接服务器 里添加

    方式二:

    相同实例的情况下

    直接select * from [AAA]..Table1 a inner join [BBB]..Table2 b on a.id1 = b.id2

    在数据库名和表名之间放两个点或者[AAA].dbo.Table1

    方式三:

    使用 OPENROWSET/OPENDATASOURCE,据说不是很稳定

    OPENDATASOURCE速度较慢

    --启用OPENROWSET/OPENDATASOURCE支持

    exec sp_configure 'show advanced options',1
    reconfigure
    exec sp_configure 'Ad Hoc Distributed Queries',1
    reconfigure
    go
    Select a.* FROM OPENROWSET('SQLOLEDB','(local)';'sa';'sa','Select * FROM [test].dbo.Student') AS a
    Select a.* FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=(local);UID=sa;PWD=sa',test.dbo.Student) AS a
    select    * From  OPENDATASOURCE('SQLOLEDB','Data Source=(local);User ID=sa;Password=richen').test.dbo.Student
    go

    --安全起见,关闭OPENROWSET/OPENDATASOURCE支持
    exec sp_configure 'Ad Hoc Distributed Queries',0
    reconfigure
    exec sp_configure 'show advanced options',0
    reconfigure
    go

  • 相关阅读:
    SQL Server连接Oracle详细步骤
    SQLServer2012连接mysql5.5
    SQL Server的链接服务器技术
    2键盘录入Scanner
    1标识符
    电脑从新分盘(软件)
    Tomcat安装配置
    windows下安装和配置多个版本的JDK
    Myeclipse2014的安装
    Could not find acceptable representation
  • 原文地址:https://www.cnblogs.com/djian/p/1845621.html
Copyright © 2011-2022 走看看