I need to execute a SQL command to give an Update to a table, The intention of this update would be to update a column with a sequential number per company, Ex: I have a table Sale in this table I have the column CompanyId for each company I intend to create a sale sequence type Company1 sale 1,2,3 ... Company 2 sale 1,2,3 and so the ID does not respect this order, For Well the problem is in the following, I created the column and need to give an update that does 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 is the EnterpriseID = 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
}