3
I need to execute a command SQL to update a table, the intention of this update would be to update a column with sequential number per company, e.g.: I have a table Sale in this table have the column Empresaid for each company I intend to create a sequence of sale type Business1 sale 1,2,3... Company 2 sale 1,2,3 and so the ID does not respect this order, well the problem is in the following, I created the column and need to give an update to do this for the records that already exist, I got with the following code:
DECLARE @NumeroSequencia int
SET @NumeroSequencia = 0
UPDATE Venda
SET @NumeroSequencia = NumeroSequencia = @NumeroSequencia + 1 where
EmpresaID = 1
But where Empresaid = 1 I wanted to do something like a foreach in the example but in Sql:
--MONTA UMA LISTA COM OS IDS DE CADA EMPRESA CADASTRADA
declare listaEmpresas = select id from empresa
--PARA CADA EMPRESA EXECUTA O UPDATE COM A SEQUENCIA DAS VENDAS
foreach (var item in listaEmpresas){
DECLARE @NumeroSequencia int
SET @NumeroSequencia = 0
UPDATE Venda
SET @NumeroSequencia = NumeroSequencia = @NumeroSequencia + 1 where
EmpresaID = item --AQUI SERIA O ID DE CADA EMPRESA SEGUINDO O FOREACH
}