I working with a group of databases with several projects of the same structure on each database. My goal is to query the master database on each server and pull the same information from all projects to aggregate and perform analysis. In SQL Server I have put together a working query (example below) which loops through all of the projects returning the same information:
Declare @DBNames Table (RN int, Name varchar(max))
Insert Into @DBNames (RN, Name)
Select Row_Number() Over (Order By Name Desc) as RN,
Name
From sys.databases
Where (Name = 'proj1' or
Name = 'proj2' or
Name = 'proj3')
and state_desc = 'Online'
-- sql part 1a
DECLARE @sql1 varchar(max) =
'
select ''a.colA
,b.colB
,c.colC
from '
-- sql2 part
Declare @SQL2 varchar(max) = '.dbo.tableA as a
inner join '
-- sql3 part
Declare @SQL3 varchar(max) = '.dbo.tableB as b on b.x = a.x
inner join '
-- sql4 part
Declare @SQL4 varchar(max) = '.dbo.tableC as c on c.y = b.y
where col1 is not null'
Declare @SQL varchar(max)
Select @SQL = (
Select Stuff((@SQL1 + Name + @SQL2 + NAME + @SQL3 + Name + @SQL4 + Case When RN = 1 Then '' Else ' Union ' End), 1, 0, '') as SQL
From @DBNames Order By RN Desc
for XML Path(''), TYPE).value('.','varchar(max)')
exec(@sql)
When I try to load the data using the above query in RODBC, it doesn't seem to work. Does RODBC support this type of query? If not, what might be a good alternative?
Thank you in advance for your time and patience!
set nocount onat the top of the batch.