SQL - Select 来自多个数据库
SQL - Select from Multiple databases
我有 10 个数据库都在同一个实例和模式中,具有相同的表。
希望创建一个 SELECT
查询,它可以使用数据库名称变量和 return 所有记录到一个数据集中。
我一直在四处寻找并将以下内容放在一起作为测试(真正的 select 语句要大得多,有多个连接)
此测试确实有效,但它 return 有两个结果集,每个数据库一个。
有没有办法将结果合并为一组,还是我使用了错误的方法?
提前致谢
DECLARE @DB_NAME VARCHAR(6);
DECLARE CURSOR_ALLDB_NAMES CURSOR FOR
SELECT name
FROM sys.databases
WHERE name IN ('CN2DAT', 'AU1DAT')
OPEN CURSOR_ALLDB_NAMES
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
WHILE @@Fetch_Status = 0
BEGIN
EXEC ('SELECT * FROM ' + @DB_NAME + '.dbo.ICITEM')
-- EXEC ('USE '+ @DB_NAME + ' SELECT * from dbo.ICITEM')
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
END
CLOSE CURSOR_ALLDB_NAMES
DEALLOCATE CURSOR_ALLDB_NAMES
我将创建一个结合了 select 语句的视图。例如
CREATE VIEW v_ICITEM
AS
SELECT * FROM CN2DAT.dbo.ICITEM
UNION ALL
SELECT * FROM AU1DAT.dbo.ICITEM
go;
您还可以将源数据库作为列包含在内:
CREATE VIEW v_ICITEM
AS
SELECT 'CN2DAT' AS Db, * FROM CN2DAT.dbo.ICITEM
UNION ALL
SELECT 'AU1DAT', * FROM AU1DAT.dbo.ICITEM
go;
将结果存储到临时文件中 table 例如:
CREATE TABLE #TempICITEM
([DbName] [varchar](6) NOT NULL,
[ICITEMId] [uniqueidentifier] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Description] [varchar](1000) NOT NULL)
将您的 TSQL select 更改为 Insert Into #TempTable Select 例如:
EXEC ('INSERT INTO #TempICITEM ([DbName],[ICITEMId],[Name],[Description]) SELECT ''' + @DB_NAME + ''' DbName,[DbName],[ICITEMId],[Name],[Description] FROM ' + @DB_NAME + '.dbo.ICITEM')
然后 Select 来自临时 table 的组合结果,例如:
SELECT * From #TempICITEM
一起:
CREATE TABLE #TempICITEM
([DbName] [varchar](6) NOT NULL,
[ICITEMId] [uniqueidentifier] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Description] [varchar](1000) NOT NULL)
DECLARE @DB_NAME VARCHAR(6);
DECLARE CURSOR_ALLDB_NAMES CURSOR FOR
SELECT name
FROM sys.databases
WHERE name IN ('CN2DAT', 'AU1DAT')
OPEN CURSOR_ALLDB_NAMES
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
WHILE @@Fetch_Status = 0
BEGIN
EXEC ('INSERT INTO #TempICITEM ([DbName],[ICITEMId],[Name],[Description]) SELECT ''' + @DB_NAME + ''' DbName,[DbName],[ICITEMId],[Name],[Description] FROM ' + @DB_NAME + '.dbo.ICITEM')
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
END
CLOSE CURSOR_ALLDB_NAMES
DEALLOCATE CURSOR_ALLDB_NAMES
SELECT * From #TempICITEM
我有 10 个数据库都在同一个实例和模式中,具有相同的表。
希望创建一个 SELECT
查询,它可以使用数据库名称变量和 return 所有记录到一个数据集中。
我一直在四处寻找并将以下内容放在一起作为测试(真正的 select 语句要大得多,有多个连接)
此测试确实有效,但它 return 有两个结果集,每个数据库一个。
有没有办法将结果合并为一组,还是我使用了错误的方法?
提前致谢
DECLARE @DB_NAME VARCHAR(6);
DECLARE CURSOR_ALLDB_NAMES CURSOR FOR
SELECT name
FROM sys.databases
WHERE name IN ('CN2DAT', 'AU1DAT')
OPEN CURSOR_ALLDB_NAMES
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
WHILE @@Fetch_Status = 0
BEGIN
EXEC ('SELECT * FROM ' + @DB_NAME + '.dbo.ICITEM')
-- EXEC ('USE '+ @DB_NAME + ' SELECT * from dbo.ICITEM')
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
END
CLOSE CURSOR_ALLDB_NAMES
DEALLOCATE CURSOR_ALLDB_NAMES
我将创建一个结合了 select 语句的视图。例如
CREATE VIEW v_ICITEM
AS
SELECT * FROM CN2DAT.dbo.ICITEM
UNION ALL
SELECT * FROM AU1DAT.dbo.ICITEM
go;
您还可以将源数据库作为列包含在内:
CREATE VIEW v_ICITEM
AS
SELECT 'CN2DAT' AS Db, * FROM CN2DAT.dbo.ICITEM
UNION ALL
SELECT 'AU1DAT', * FROM AU1DAT.dbo.ICITEM
go;
将结果存储到临时文件中 table 例如:
CREATE TABLE #TempICITEM
([DbName] [varchar](6) NOT NULL,
[ICITEMId] [uniqueidentifier] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Description] [varchar](1000) NOT NULL)
将您的 TSQL select 更改为 Insert Into #TempTable Select 例如:
EXEC ('INSERT INTO #TempICITEM ([DbName],[ICITEMId],[Name],[Description]) SELECT ''' + @DB_NAME + ''' DbName,[DbName],[ICITEMId],[Name],[Description] FROM ' + @DB_NAME + '.dbo.ICITEM')
然后 Select 来自临时 table 的组合结果,例如:
SELECT * From #TempICITEM
一起:
CREATE TABLE #TempICITEM
([DbName] [varchar](6) NOT NULL,
[ICITEMId] [uniqueidentifier] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Description] [varchar](1000) NOT NULL)
DECLARE @DB_NAME VARCHAR(6);
DECLARE CURSOR_ALLDB_NAMES CURSOR FOR
SELECT name
FROM sys.databases
WHERE name IN ('CN2DAT', 'AU1DAT')
OPEN CURSOR_ALLDB_NAMES
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
WHILE @@Fetch_Status = 0
BEGIN
EXEC ('INSERT INTO #TempICITEM ([DbName],[ICITEMId],[Name],[Description]) SELECT ''' + @DB_NAME + ''' DbName,[DbName],[ICITEMId],[Name],[Description] FROM ' + @DB_NAME + '.dbo.ICITEM')
FETCH NEXT FROM CURSOR_ALLDB_NAMES INTO @DB_NAME
END
CLOSE CURSOR_ALLDB_NAMES
DEALLOCATE CURSOR_ALLDB_NAMES
SELECT * From #TempICITEM