Here's a dynamic SQL example (e.g. build the query as a string and then use built in procedure sp_executesql to execute the query).
This example uses STUFF and FOR XML PATH('') to concatenate a bunch of queries together and stick ' UNION ' between each one.
DECLARE @sql NVARCHAR(MAX) = 'INSERT INTO TestTable (col1, col2) '
SELECT @sql += STUFF(
(SELECT ' UNION SELECT col1, col2 FROM ' + t.name
FROM sys.tables as t
WHERE t.name like 'Fast_a%'
FOR XML PATH('')), 1, 7, '')
EXEC sp_executesql @sql
So, for this schema:
CREATE TABLE fast_a1 (col1 INT, col2 INT)
CREATE TABLE fast_a2 (col1 INT, col2 INT)
CREATE TABLE fast_aasdf (col1 INT, col2 INT)
CREATE TABLE TestTable (col1 INT, col2 INT)
It builds this dynamic query:
INSERT INTO TestTable (col1, col2)
SELECT col1, col2 FROM fast_a1
UNION
SELECT col1, col2 FROM fast_a2
UNION
SELECT col1, col2 FROM fast_aasdf
Edit:
Liz, run these statements (or here's a SQL fiddle) in sequence, and you'll see that they do insert data:
CREATE TABLE fast_a1 (col1 INT, col2 INT);
CREATE TABLE fast_a2 (col1 INT, col2 INT);
CREATE TABLE fast_aasdf (col1 INT, col2 INT);
CREATE TABLE TestTable (col1 INT, col2 INT);
INSERT INTO dbo.fast_a1 VALUES (1, 1);
INSERT INTO dbo.fast_a2 VALUES (2, 2);
INSERT INTO dbo.fast_aasdf VALUES (3, 3);
DECLARE @sql NVARCHAR(MAX) = 'INSERT INTO TestTable (col1, col2) '
SELECT @sql += STUFF(
(SELECT ' UNION SELECT col1, col2 FROM ' + t.name
FROM sys.tables as t
WHERE t.name like 'Fast_a%'
FOR XML PATH('')), 1, 7, '')
EXEC sp_executesql @sql
SELECT * FROM dbo.TestTable
Returns:
col1 col2
1 1
2 2
3 3