【问题标题】:How to join two tables from different database in MS SQL Azure?如何在 MS SQL Azure 中连接来自不同数据库的两个表?
【发布时间】:2019-05-16 08:55:11
【问题描述】:

我在两个不同的数据库中有两个不同的表。需要同时加入表并使用查询获取结果

1) 报告 - report_id、report_name、描述

表报告存在于数据库“A”中

2) report_owners - report_id、report_owner

表 report_owners 存在于数据库“B”中

尝试了以下查询以加入表,但它抛出错误(此版本的 SQL 服务器不支持对服务器和/数据库的引用)。

查询:

select * from [A].[dbo].['reports'] as all_reports
INNER JOIN
select * from [B].[dbo].['report_owners'] as all_owner
ON all_report.report_id = all_owners.report_id

注意:从数据库“A”可以访问数据库“B”的交叉查询功能

谁能帮我解决这个问题?

【问题讨论】:

标签: sql-server azure-sql-database


【解决方案1】:

您需要使用elastic queries 来执行跨数据库查询。请参阅以下示例:

客户信息(远程数据库)

CREATE TABLE dbo.CustomerInformation (CustomerID int identity(1,1) , CustomerName nvarchar(255));

DECLARE @i int = 1;
WHILE @i < 20000
BEGIN
INSERT INTO CustomerInformation(CustomerName)
VALUES ('Frodo');

SET @i += 1;
END

在将使用外部表的数据库上

CREATE TABLE OrderInformation(OrderId Int identity(1,1), CustomerId int,Ordervalue int);


SET NOCOUNT ON;
DECLARE @i int = 1;
WHILE @i < 200 
BEGIN
INSERT INTO OrderInformation(CustomerId,Ordervalue)
Values (@I,111);

set @i += 1;
END

CREATE EXTERNAL TABLE  [dbo].CustomerInformation(
    CustomerID [int],
CustomerName nvarchar(255))
    WITH  
(  
    DATA_SOURCE = Stackoverflow  
); 

这样查询远程表。

SELECT o.OrderId, c.CustomerID, c.CustomerName
  FROM OrderInformation o
  JOIN CustomerInformation c ON o.CustomerID = c.CustomerID
 WHERE o.OrderId = 155

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 2020-11-05
    • 2014-10-05
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多