如何组合多个动态查询

时间:2017-03-28 04:24:55

标签: sql sql-server dynamic-sql dynamicquery

问题通过我自己的答案解决了

我指的是How to combine two dynamic sql with union ALL,但它不起作用

enter image description here

我的查询问题是union方法无法工作,因为它没有从变量中获取数据,因为它是alraedy执行的事实:exec(insert1)和exec (insert2)

和union的目的是通过选择' table_catalog'来获取db_name。之后,因为如果' table_catalog'在比较之前选择,输出将是错误的

这里是查询

    Declare @PilotDBName nvarchar(255)
Declare @LiveDBName nvarchar(255)

Declare @Table1 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
Declare @Table2 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table3 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table4 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table5 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table6 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255), table_catalog nvarchar(255))  
Declare @Table7 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255), table_catalog nvarchar(255)) 

Set @PilotDBName = 'LON_HRMS'
Set @LiveDBName = 'VSM641SP3QC'

Declare @insert1 NVARCHAR(MAX) 
set @insert1 = '
Declare @Table1 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
INSERT INTO @Table1
SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME, b.table_type
FROM '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@PilotDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE''

EXCEPT 

SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME,b.table_type
FROM '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@LiveDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE'' ORDER BY VIEW_NAME '
EXEC(@insert1)

Declare @insert2 NVARCHAR(MAX)
Declare @SQLResult NVARCHAR(MAX)
Set @insert2 = '
Declare @Table2 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
INSERT INTO @Table2
SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME, b.table_type
FROM '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@LiveDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE''

EXCEPT 

SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME,b.table_type
FROM '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@PilotDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE'' ORDER BY VIEW_NAME '
Exec(@insert2)


Declare @insert5 NVARCHAR(MAX) = '
Declare @Table1 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
Declare @Table2 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
Declare @Table5 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
Insert into @Table5
select * From Table1
Union
select * from @Table2'
Exec(@insert1)Exec(@insert2)Exec(@insert5)

在显示输出之前,查询中的记录是:

  

插入1:243行   插入2:1行   插入5:0行

enter image description here

1 个答案:

答案 0 :(得分:-1)

我自己找到了答案,基本上我所要做的就是把所有东西都扔进一大块动态sql

并且表变量必须在中声明,而其余的可以将其留在动态查询之外

Declare @PilotDBName nvarchar(255)
Declare @LiveDBName nvarchar(255)
Set @PilotDBName = 'LON_HRMS'
Set @LiveDBName = 'VSM641SP3QC'

Declare @insert1 NVARCHAR(MAX) = '
Declare @Table1 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255)) 
Declare @Table2 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table3 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table4 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255))  
Declare @Table5 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255), Table_In nvarchar(255))  
Declare @Table6 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255), Table_In nvarchar(255))  
Declare @Table7 Table(TABLE_SCHEMA nvarchar(255)  , VIEW_NAME nvarchar(255), COLUMN_NAME nvarchar(255), table_type nvarchar(255), Table_In nvarchar(255)) 


INSERT INTO @Table1
SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME, b.table_type
FROM '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@PilotDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE''

EXCEPT 

SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME,b.table_type
FROM '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@LiveDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE'' ORDER BY VIEW_NAME 


INSERT INTO @Table2
SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME, b.table_type
FROM '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@LiveDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE''

EXCEPT 

SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME,b.table_type
FROM '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@PilotDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''BASE TABLE'' ORDER BY VIEW_NAME 

INSERT INTO @Table3
SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME, b.table_type
FROM '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@PilotDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''VIEW''

EXCEPT 

SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME,b.table_type
FROM '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@LiveDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''VIEW'' ORDER BY VIEW_NAME  

INSERT INTO @Table4
SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME, b.table_type
FROM '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@LiveDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''VIEW''

EXCEPT 

SELECT a.TABLE_SCHEMA, a.VIEW_NAME, COLUMN_NAME,b.table_type
FROM '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE a Inner join '+@PilotDBName+'.information_schema.tables b 
on a.TABLE_NAME = b.TABLE_NAME
where b.table_type=''VIEW'' ORDER BY VIEW_NAME  

Insert into @Table5
select a.TABLE_SCHEMA, a.VIEW_NAME, a.COLUMN_NAME As Column_Missing,a.table_type, info.TABLE_CATALOG As Found_In  from @Table1 a , '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE info
Union
select b.TABLE_SCHEMA, b.VIEW_NAME, b.COLUMN_NAME As Column_Missing,b.table_type, info.TABLE_CATALOG As Found_In from @Table2 b , '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE info
order by table_catalog

Insert into @Table6
select a.TABLE_SCHEMA, a.VIEW_NAME, a.COLUMN_NAME As Column_Missing,a.table_type, info.TABLE_CATALOG As Found_In  from @Table3 a , '+@PilotDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE info
Union 
select b.TABLE_SCHEMA, b.VIEW_NAME, b.COLUMN_NAME As Column_Missing,b.table_type, info.TABLE_CATALOG As Found_In  from @Table4 b , '+@LiveDBName+'.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE info


select * from @Table5 a 
Union 
select * from @Table6 b '
Exec(@insert1)