-- =============================================
-- Author:		Atzeri Emanuele
-- Create date: <Create Date,,>
-- Description:	<Adjusted to FEU>
-- =============================================
CREATE procedure [dbo].[srv_change_codfiscaleMassiva_CU]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	DECLARE @Vecchio varchar(16)
	DECLARE @Nuovo varchar(16)
	DECLARE @DataIns datetime
	DECLARE @isCodFis int
	Declare @contErr int
	Declare @CodErr int
	Declare @retDescErr varchar(500)
	set @CodErr= 1
	set @contErr= 0
	DECLARE Ricodifica_cursor CURSOR FOR
	SELECT CF_OLD, CF_NEW, DTINSERIMENTO from RICODIF_CF_PIVA_CU
	ORDER BY DTINSERIMENTO
	OPEN Ricodifica_cursor
	FETCH NEXT FROM Ricodifica_cursor
	INTO @Vecchio, @Nuovo, @DataIns
	WHILE @@FETCH_STATUS = 0
	BEGIN		
		select @isCodfis=dbo.RegExIsMatch ( '\D' , @Nuovo , 1 )
		EXEC dbo.srv_change_codfiscale_CU @Nuovo, @Vecchio , @isCodFis , @retcodErr=@CodErr  output
		if @CodErr = 0 
		begin
				UPDATE [CONSUNI].ConsulenzaUnicaFL.dbo.RICODIF_CF_PIVA
				SET RicodificatoContratto6 = 1
				WHERE
					CF_OLD = @Vecchio
				AND
					CF_NEW = @Nuovo
				AND
					DTINSERIMENTO = @DataIns
				AND
					RicodificatoContratto6 is null
		end
		else
		begin 
		  select @CodErr, 'Errore '
		  set @contErr=@contErr+1;
	    end
	FETCH NEXT FROM Ricodifica_cursor
		INTO @Vecchio, @Nuovo, @DataIns
	END
	CLOSE Ricodifica_cursor
	DEALLOCATE Ricodifica_cursor
	select @contErr as 'result'
	return @contErr
END