--Rodar o script até nao aparecer mais lotes na lista. --Caso nao haja alteração, e/ou apareça 2 lotes abaixo, você deve alterar manualmente pois são lotes de carvao. declare @almoxarifado int; declare @material int; declare @idlote int; set @almoxarifado = (select top 1 AlmoxarifadoDocumento from DocumentoEstoqueProducao where LoteDocumento is null ) set @material = (select top 1 materiallote from LoteProducao where AlmoxarifadoLote = @almoxarifado) set @idlote = (select top 1 autoid from LoteProducao where AlmoxarifadoLote = @almoxarifado) update documentoestoqueproducao set lotedocumento=@idlote WHERE (DocumentoEstoqueProducao.LoteDocumento IS NULL) and AlmoxarifadoDocumento = @almoxarifado and MaterialMovimento = @material select AutoId,AlmoxarifadoLote,MaterialLote from LoteProducao where AlmoxarifadoLote = @almoxarifado