and my part of the project got simpler sort of. they no longer want the output to a file. just send results to a temp table and issue a select * from #temptable and they would copy the results to a spreadsheet that multiple users will be pasting the data into.
they have about 30 SQL servers to run it on with unknown number of sql databases to gather the info from.
got this far into said script, I am still working on it but so far it is not producing the temptable and i am going to add a if table1 exists logic so i don't query databases without the table1 in them.
DECLARE @TABLESCRIPT VARCHAR(MAX), @DATABASENAME NVARCHAR(128);
set @DATABASENAME = ''
WHILE @DATABASENAME IS NOT NULL
SET @DATABASENAME = (SELECT MIN(QUOTENAME(name))
WHERE QUOTENAME(name) > @DATABASENAME)
SET @TABLESCRIPT = '(SELECT '''+@DATABASENAME+''' AS ref_database, ust_date / 10000 as ref_yr, SUM(ust_amt) as ref_amt, COUNT(*) AS ref_count INTO #TEMPTABLEE
FROM ' +@DATABASENAME+'..[TABLE1]
WHERE (ust_type = ''R'') and (ust_date / 10000 in (2009, 2008, 2007, 2006, 2005))
GROUP BY (ust_date / 10000))'
SELECT * FROM #TEMPTABLE