declare @c cursor as
select name,
case
when substring(name,2,1) between '0' and '9' then 1990 + cast(substring(name,2,1) as SQL_INTEGER)
else 2000 +
Ascii(substring(name,2,1)) - 65
end as Jahr
from rechnungen;
open @c;
try
drop table #temp;
catch all
end try;
create table #temp (Name char(8), Id integer);
while fetch @c do
execute immediate
'insert into #temp (Name, Id, Umsatz) '+
'select name, b.id'+
'from rechnungen inner join '+
-- Hier wird der Name der Tabelle aus zwei Feldern des erzeugten Cursors gebildet
-- In der gejointen Tabelle steht das Feld Umsatz
'(select top 1 * from ['+Trim(convert(@c.Jahr, SQL_CHAR))+'\'+Trim(@c.name)+']) B '+
'on True '+
'where rechnungen.name = '''+@c.name+''';';
end while;
close @c;
-- Rückgabe-Datenmenge
select * from #temp;