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

  • 相关阅读:
    mysql orderby 查询过慢优化
    js密码复杂度验证
    下载linux指定目录下的文件
    关于对接农业银行支付的问题
    string拼接时去掉最后一个逗号
    java基础题整理(1)
    springboot 读取 resource文件
    自动生成Excel 报表工具类
    java设计模式—— 工厂模式
    pyqt线程实现
  • 原文地址:https://www.cnblogs.com/djian/p/1845621.html
Copyright © 2011-2022 走看看