I have a temporary table that I fill in with the result of a given query.
I need to move the data from this temporary table, line by line, and pick certain fields and insert them into another table, and in that other table, generate a coupon code, with a specific sequence, to complete the process.
I can not simply use the INSERT INTO ... SELECT (as I use in my script in other points)
How can I make this loop catch line-by-line from temporary table?
CREATE TABLE #PontosVPAcimaCem (
qtd_cupons INT
,apuracao_ptsvp NUMERIC(14, 2)
,apuracao_mesfch INT
,apuracao_anofch INT
,apuracao_id_client INT
,clients_username NVARCHAR(150)
)
INSERT INTO #PontosVPAcimaCem (
qtd_cupons
,apuracao_ptsvp
,apuracao_mesfch
,apuracao_anofch
,apuracao_id_client
,clients_username
)
SELECT CAST(a.ptsvp / 100 AS INT)
,a.ptsvp
,a.mesfch
,a.anofch
,a.id_client
,c.username
FROM t_clients c WITH (NOLOCK)
INNER JOIN gr_apuracao a WITH (NOLOCK) ON c.id = a.id_client
WHERE a.mesfch = @apuracao_mes
AND a.anofch = @apuracao_ano
AND a.ptsvp >= @apuracao_pontosvp
SELECT qtd_cupons
,apuracao_ptsvp
,apuracao_mesfch
,apuracao_anofch
,apuracao_id_client
,clients_username
FROM #PontosVPAcimaCem WITH (NOLOCK)
ORDER BY qtd_cupons DESC