DECLARE @DataInicial AS DATETIME
	, @DataFinal AS DATETIME
	, @Filial AS VARCHAR(2)

SELECT @DataInicial = '20100101'
	, @DataFinal = '20100308'
	, @Filial = '01'

BEGIN TRANSACTION
UPDATE NF
SET cfo = cf.cfo  
FROM NFEntraCad NF
INNER JOIN CFOEntrCAD CF ON NF.numord = cf.numord
	AND CF.cfo = (SELECT TOP 1 cfo from CFOENTRCAD CF1 WHERE cf1.numord = CF.numord AND CF.cfo <> '')
WHERE CF.cfo <> ''
	AND NF.dtemis BETWEEN @DataInicial AND @DataFinal
	AND NF.Filial = @Filial

COMMIT
