我在字符串设置中有2个select语句,如下所示:
set @sql = 'Select.......' -- (Returns col1, col2, col3)
exec (@sql)
set @sql = 'Select.......' -- (Returns col4, col5, col6)
exec (@sql)
我想加入这两个exec语句,以便列显示为结果:
col1 | col2 | col3 | col4 | col5 | col6
任何提示?感谢。
答案 0 :(得分:1)
在本地服务器上启用DATA ACCESS:
exec sp_serveroption @server = 'YourServerName'
,@optname = 'DATA ACCESS'
,@optvalue = 'TRUE'
并使用OPENQUERY:
select
*
from (
select * from openquery(YourServerName, 'select 1 as a')
) t1
full join (
select * from openquery(YourServerName, 'select 3 as b')
) t2
on t1.a = t2.b
答案 1 :(得分:0)
DECLARE @SQL1 VARCHAR(MAX)
DECLARE @SQL2 VARCHAR(MAX)
SET @SQL1='SELECT COL1,COL2,NULL AS COL3,NULL AS COL4 FROM #TEST1'
SET @SQL2='SELECT NULL,NULL, COL3, COL4 FROM #TEST2'
EXEC (@SQL1 +' UNION ALL '+ @SQL2)
答案 2 :(得分:0)
我通过删除EXEC并将它们保持为普通的Select语句得到了我想要的结果。然后我执行了内部连接以避免NULLS。谢谢大家的帮助。