1
I have a temporary table that fills it with the result of a particular query. Now I need to go through the data of this temporary table, row by row, and take certain fields and insert them into another table, and in this other table, generate a coupon code, with a specific sequence, to complete the process.
How can I make this loop to capture row by row from the 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