按列连接数据库,其中数据驻留在不同的数据库中
join databases per column where data resides in different databases
我正在尝试从位于不同数据库但具有相同架构的 table 中获取数据。
I.E 数据库名称类似于:DB1、DB2、DB3。
它们都包含 table 'MyTable' 并且有一个外部 KEY 与我们称为 MainDB 的数据库中的 'MainTable' 主 KEY 匹配,我会这样做一个联合并担心以后的性能,但问题是,在这个特定的实例中,有时 DB3(或任何一个)不存在,这会导致我的查询失败,需要每天编辑。
MainTable 有一些列,这些列组合起来构成了我需要在每一列上加入的数据库的名称,为简单起见,我将其称为 ForeignDBName 列。
我想要类似
的东西
SELECT
MainPK
, FName
, LName
, BirthDate
, ForeignDBName
FROM
MainDB.dbo.MainTable
LEFT JOIN
(
SELECT
BirthDate
,MainTableFK
FROM
DB1.dbo.MyTable
UNION ALL
SELECT
BirthDate
,MainTableFK
FROM
DB2.dbo.MyTable
UNION ALL
SELECT
BirthDate
,MainTableFK
FROM
DB3.dbo.MyTable
) B ON B.MainTableFK = MainPK
因为您不知道实际存在多少您需要的数据库,所以您需要 运行 一些动态 SQL:
DECLARE @statement nvarchar(2048);
DECLARE @db_name varchar(128);
SET @statement='SELECT
MainPK
, FName
, LName
, BirthDate
, ForeignDBName
FROM
MainDB.dbo.MainTable LEFT JOIN ('
DECLARE db_cursor CURSOR FOR
--adjust database names here
select [name] from sys.databases where [name] in ('DB1','DB2','DB3','DB4','DB5')
ORDER BY name
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @db_name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @statement=@statement+CHAR(10)+'SELECT BirthDate,MainTableFK FROM '+@db_name+'.dbo.MyTable UNION ALL ';
FETCH NEXT FROM db_cursor INTO @db_name
END
CLOSE db_cursor;
DEALLOCATE db_cursor;
-- we remove the last UNION ALL from the statement
SET @statement=LEFT(@statement,LEN(@statement) - 10)
SET @statement=@statement+') B ON B.MainTableFK = MainPK'
-- comment this to hide the statement that will be executed
print @statement;
--uncomment this if you want to actually execute the statement
--exec sp_executesql @statement
我正在尝试从位于不同数据库但具有相同架构的 table 中获取数据。
I.E 数据库名称类似于:DB1、DB2、DB3。
它们都包含 table 'MyTable' 并且有一个外部 KEY 与我们称为 MainDB 的数据库中的 'MainTable' 主 KEY 匹配,我会这样做一个联合并担心以后的性能,但问题是,在这个特定的实例中,有时 DB3(或任何一个)不存在,这会导致我的查询失败,需要每天编辑。
MainTable 有一些列,这些列组合起来构成了我需要在每一列上加入的数据库的名称,为简单起见,我将其称为 ForeignDBName 列。 我想要类似
的东西SELECT
MainPK
, FName
, LName
, BirthDate
, ForeignDBName
FROM
MainDB.dbo.MainTable
LEFT JOIN
(
SELECT
BirthDate
,MainTableFK
FROM
DB1.dbo.MyTable
UNION ALL
SELECT
BirthDate
,MainTableFK
FROM
DB2.dbo.MyTable
UNION ALL
SELECT
BirthDate
,MainTableFK
FROM
DB3.dbo.MyTable
) B ON B.MainTableFK = MainPK
因为您不知道实际存在多少您需要的数据库,所以您需要 运行 一些动态 SQL:
DECLARE @statement nvarchar(2048);
DECLARE @db_name varchar(128);
SET @statement='SELECT
MainPK
, FName
, LName
, BirthDate
, ForeignDBName
FROM
MainDB.dbo.MainTable LEFT JOIN ('
DECLARE db_cursor CURSOR FOR
--adjust database names here
select [name] from sys.databases where [name] in ('DB1','DB2','DB3','DB4','DB5')
ORDER BY name
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @db_name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @statement=@statement+CHAR(10)+'SELECT BirthDate,MainTableFK FROM '+@db_name+'.dbo.MyTable UNION ALL ';
FETCH NEXT FROM db_cursor INTO @db_name
END
CLOSE db_cursor;
DEALLOCATE db_cursor;
-- we remove the last UNION ALL from the statement
SET @statement=LEFT(@statement,LEN(@statement) - 10)
SET @statement=@statement+') B ON B.MainTableFK = MainPK'
-- comment this to hide the statement that will be executed
print @statement;
--uncomment this if you want to actually execute the statement
--exec sp_executesql @statement