-- Schema: C6Mart
-- Stored Procedure: Controlli_RepGiornaliera












-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================

CREATE  procedure [C6Mart].[Controlli_RepGiornaliera]
AS
BEGIN

--------------------------
--prima parte
--------------------------

		drop table if exists C6Mart.clientidasistemare
		drop table if exists c6mart.TB_ClienticonCaratteriAnomali
		drop table if exists #tab_temp
		declare @num int 
		set @num = 1

		CREATE TABLE #tab_temp(
			[Val_Ascii] [smallint] NOT NULL,
			[Val_Char] [varchar](1) NOT NULL,
			[Rete] [varchar](1) NOT NULL,
			[Cod_Fiscale] [varchar](16) NOT NULL,
			[Nome_Campo] [varchar](50) NOT NULL,
			[Val_Campo] [varchar](150) NOT NULL)


		set @num = 1
		while @num < 150

		begin	
		insert into #tab_temp
		SELECT ascii(substring(email,@num,1)),
			   substring(email,@num,1),
			   rete,
			   Cod_Fiscale,
			   'EMAIL',
			   email
		FROM C6Mart.vcontratti
		WHERE 
			ascii(substring(email,@num,len(email))) not between 64 and 90 
		and ascii(substring(email,@num,len(email))) not between 97 and 122
		and ascii(substring(email,@num,len(email))) not between 48 and 57
		and ascii(substring(email,@num,len(email))) <> 32
		and ascii(substring(email,@num,len(email))) <> 40
		and ascii(substring(email,@num,len(email))) <> 41
		and ascii(substring(email,@num,len(email))) <> 43
		and ascii(substring(email,@num,len(email))) <> 44
		and ascii(substring(email,@num,len(email))) <> 45
		and ascii(substring(email,@num,len(email))) <> 46
		and ascii(substring(email,@num,len(email))) <> 95
		and ascii(substring(email,@num,len(email))) <> 39
		and ascii(substring(email,@num,len(email))) <> 47
		and ascii(substring(email,@num,len(email))) <> 63


		set @num = @num + 1	
		end	

		--inizio intervento: 05/02/2015 FCianfa Controllo caratteriAnomali su campi Nome e Cognome della tb c6mart.ANAG_CLIENTI
		set @num = 1
		while @num < 150

		begin	
		insert into #tab_temp
		SELECT ascii(substring(Nome,@num,1)),
			   substring(Nome,@num,1),
			   rete,
			   Cod_Fiscale,
			   'Nome',
			   Nome
		FROM C6Mart.ANAG_CLIENTI
		WHERE 
			ascii(substring(Nome,@num,len(Nome))) not between 64 and 90 
		and ascii(substring(Nome,@num,len(Nome))) not between 97 and 122
		and ascii(substring(Nome,@num,len(Nome))) not between 48 and 57
		and ascii(substring(Nome,@num,len(Nome))) <> 32
		and ascii(substring(Nome,@num,len(Nome))) <> 40
		and ascii(substring(Nome,@num,len(Nome))) <> 41
		and ascii(substring(Nome,@num,len(Nome))) <> 43
		and ascii(substring(Nome,@num,len(Nome))) <> 44
		and ascii(substring(Nome,@num,len(Nome))) <> 45
		and ascii(substring(Nome,@num,len(Nome))) <> 46
		and ascii(substring(Nome,@num,len(Nome))) <> 95
		and ascii(substring(Nome,@num,len(Nome))) <> 39
		and ascii(substring(Nome,@num,len(Nome))) <> 38
		and ascii(substring(Nome,@num,len(Nome))) <> 47
		and ascii(substring(Nome,@num,len(Nome))) <> 63


		set @num = @num + 1	
	
		end	
		--aggiunta 30/07/2022 per il cognome
		set @num = 1
		while @num < 150
		begin	
		insert into #tab_temp
		SELECT ascii(substring(Cognome,@num,1)),
			   substring(Cognome,@num,1),
			   rete,
			   Cod_Fiscale,
			   'Cognome',
			   Cognome
		FROM C6Mart.ANAG_CLIENTI
		WHERE 
			ascii(substring(Cognome,@num,len(Cognome))) not between 64 and 90 
		and ascii(substring(Cognome,@num,len(Cognome))) not between 97 and 122
		and ascii(substring(Cognome,@num,len(Cognome))) not between 48 and 57
		and ascii(substring(Cognome,@num,len(Cognome))) <> 32
		and ascii(substring(Cognome,@num,len(Cognome))) <> 40
		and ascii(substring(Cognome,@num,len(Cognome))) <> 41
		and ascii(substring(Cognome,@num,len(Cognome))) <> 43
		and ascii(substring(Cognome,@num,len(Cognome))) <> 44
		and ascii(substring(Cognome,@num,len(Cognome))) <> 45
		and ascii(substring(Cognome,@num,len(Cognome))) <
