Creo que ya nos estamos acercando.
Código SQL:
Ver originalDECLARE @i AS INT
SET @i = 1
while @i <= 20
BEGIN
EXEC('
Insert into tabla2 (unidades' + CAST(@i AS VARCHAR) + ')
select id' + CAST(@i AS VARCHAR) + '
from tablatemporal
where id' + CAST(@i AS VARCHAR) + ' > 0'
)
END