This is the real name fot the stored procedure that individually it works for only a week.
CREATE PROCEDURE sp_cargar_estados_anteriores AS
DECLARE @Fecha_Carga smalldatetime
--> I get the week
SELECT @Fecha_Carga = Fecha_Carga FROM ESC_FECHA_CARGA
--> Check this instruction ******FIRST INSTRUCTION********
INSERT INTO FACT_CONTRATOS
SELECT @Fecha_Carga As Fecha, T1.Id_Zona, T1.Id_Estado, T1.Id_Centro, T1.Id_Usuario, T1.Id_Contrato,
1 As Id_Tipo_Contrato, 'FP' As Id_Estado_Contrato, T1.Id_Familia, T1.Id_Marca, T1.Id_Producto, T1.Ot, 'AS' As Estad

t, T1.Fecha_Ot, NULL As Dias_Fuera_Plazo,
T1.Importe_contr, T1.Importe_Contr_Anticipo, T1.Importe_Contr_Pendiente, T1.Unidades
FROM FACT_CONTRATOS T1 join LU_CONTRATO T3 on (T1.Id_Contrato = T3.Id_Contrato)
WHERE T1.Fecha > @Fecha_Carga
AND T1.Id_Estado_Contrato in ('AF')
AND T1.Id_Tipo_Contrato not in (1)
AND DATEDIFF(day, T1.Fecha_OT, @Fecha_Carga-1) > 0
AND T3.Fecha_Fin > @Fecha_Carga
******SECOND INSTRUCTION********
INSERT INTO FACT_CONTRATOS
SELECT T2.SE_InicioSemana As Fecha, T1.Id_Zona, T1.Id_Estado, T1.Id_Centro, T1.Id_Usuario, T1.Id_Contrato,
1 As Id_Tipo_Contrato, 'AC' As Id_Estado_Contrato, T1.Id_Familia, T1.Id_Marca, T1.Id_Producto, T1.Ot, 'AS' As Estad

t, T1.Fecha_Ot, NULL As Dias_Fuera_Plazo,
T1.Importe_contr, T1.Importe_Contr_Anticipo, T1.Importe_Contr_Pendiente, T1.Unidades
FROM FACT_CONTRATOS T1 join LU_CONTRATO T3 on (T1.Id_Contrato = T3.Id_Contrato), LU_SEMANA T2
WHERE T1.Fecha = @Fecha_Carga
AND T3.Fecha_Contrata between T2.SE_InicioSemana and T2.SE_FinSemana
AND T1.Id_Estado_Contrato in ('AF')
AND T1.Id_Tipo_Contrato not in (1)
GO
CREATE PROCEDURE sp_cargar_ciclo_dwh_facts AS
DECLARE @Fecha_Inicio smalldatetime
DECLARE @Fecha_Fin smalldatetime
DECLARE @Siguiente smalldatetime
SELECT @Fecha_Inicio = MIN(Fecha_Contra) FROM ESC_CONTRATO
SELECT @Fecha_Fin = getdate()
DECLARE CUR_SEMANAS CURSOR FOR
SELECT SE_InicioSemana FROM LU_SEMANA where SE_InicioSemana between @Fecha_Inicio and @Fecha_Fin
OPEN CUR_SEMANAS
FETCH NEXT FROM CUR_SEMANAS INTO @Siguiente
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE ESC_FECHA_CARGA SET Fecha_Carga = @Siguiente
EXEC sp_cargar_dwh_facts
EXEC sp_cargar_estados_anteriores
FETCH NEXT FROM CUR_SEMANAS INTO @Siguiente
END
CLOSE CUR_SEMANAS
DEALLOCATE CUR_SEMANAS
go
If i run this procedure, the FIRST INSTRUCTION of the sp_cargar_estados_anteriores doesn't run, but the SECOND INSTRUCTION does. I don't know if the DATEDIFF instruction is doing strange things...