Build Dynamic SQL Stored Procedure from data in SQL Table Crossing dbs?

I have a table that contains the following columns:
|DatabaseName|AccountIndexDue|AccountIndexFrom|
|THX|293|8284|
|FRM|280|9238|
|MKZ|2094|72364|

I'd like to create either a stored procedure or sql view that basically looks something like this:

SELECT 'THX' as [Entity Id] * From THX..GL20000 WHERE AccountIndex in (293, 8284) UNION ALL
SELECT 'FRM' as [Entity Id] * From FRM..GL20000 WHERE AccountIndex in (280, 9238) UNION ALL
SELECT 'MKZ' as [Entity Id] * From MKZ..GL20000 WHERE AccountIndex in (2094, 72364)

I'm putting the result set into a table so ideally a Cursor Loop statement would likely work but I'm not exactly sure how to write something like that.

Suggestions?

DECLARE @sql_pattern varchar(8000)

SET @sql_pattern = 'SELECT ''$db$'' as [Entity Id] * From [$db$]..GL20000 ' +
    'WHERE AccountIndex in ($acctdue$, $acctfrom$) UNION ALL'

INSERT INTO dbo.result_table ( ... )
SELECT REPLACE(REPLACE(REPLACE(@sql_pattern,
    '$db$', tn.DatabaseName),
    '$acctdue$', CAST(AccountIndexDue AS varchar(10))),
    '$acctfrom$', CAST(AccountIndexFrom AS varchar(10)))
FROM dbo.table_name tn