PDC_REPORT_CreazioneDB/sql/Collaudo/procedure/C6Mart_Controlli_RepGiornaliera.sql
2025-06-10 15:29:00 +02:00

520 lines
18 KiB
Transact-SQL

-- =============================================
-- 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))) <> 46
and ascii(substring(Cognome,@num,len(Cognome))) <> 95
and ascii(substring(Cognome,@num,len(Cognome))) <> 39
and ascii(substring(Cognome,@num,len(Cognome))) <> 38
and ascii(substring(Cognome,@num,len(Cognome))) <> 47
and ascii(substring(Cognome,@num,len(Cognome))) <> 63
set @num = @num + 1
end
--fine aggiunta 30/07/2022 per il cognome
--fine 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(Indirizzo,@num,1)),
substring(Indirizzo,@num,1),
rete,
Cod_Fiscale,
'INDIRIZZO',
Indirizzo
FROM C6Mart.vcontratti
WHERE
ascii(substring(Indirizzo,@num,len(Indirizzo))) not between 64 and 90
and ascii(substring(Indirizzo,@num,len(Indirizzo))) not between 97 and 122
and ascii(substring(Indirizzo,@num,len(Indirizzo))) not between 48 and 57
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 32
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 40
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 41
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 43
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 44
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 45
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 46
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 95
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 39
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 47
and ascii(substring(Indirizzo,@num,len(Indirizzo))) <> 63
set @num = @num + 1
end
set @num = 1
while @num < 150
begin
insert into #tab_temp
SELECT ascii(substring(CAP,@num,1)),
substring(CAP,@num,1),
rete,
Cod_Fiscale,
'CAP',
CAP
FROM C6Mart.vcontratti
WHERE
ascii(substring(CAP,@num,len(CAP))) not between 64 and 90
and ascii(substring(CAP,@num,len(CAP))) not between 97 and 122
and ascii(substring(CAP,@num,len(CAP))) not between 48 and 57
and ascii(substring(CAP,@num,len(CAP))) <> 32
and ascii(substring(CAP,@num,len(CAP))) <> 40
and ascii(substring(CAP,@num,len(CAP))) <> 41
and ascii(substring(CAP,@num,len(CAP))) <> 43
and ascii(substring(CAP,@num,len(CAP))) <> 44
and ascii(substring(CAP,@num,len(CAP))) <> 45
and ascii(substring(CAP,@num,len(CAP))) <> 46
and ascii(substring(CAP,@num,len(CAP))) <> 95
and ascii(substring(CAP,@num,len(CAP))) <> 39
and ascii(substring(CAP,@num,len(CAP))) <> 47
and ascii(substring(CAP,@num,len(CAP))) <> 63
set @num = @num + 1
end
set @num = 1
while @num < 150
begin
insert into #tab_temp
SELECT ascii(substring(Citta,@num,1)),
substring(Citta,@num,1),
rete,
Cod_Fiscale,
'Citta',
Citta
FROM C6Mart.vcontratti
WHERE
ascii(substring(Citta,@num,len(Citta))) not between 64 and 90
and ascii(substring(Citta,@num,len(Citta))) not between 97 and 122
and ascii(substring(Citta,@num,len(Citta))) not between 48 and 57
and ascii(substring(Citta,@num,len(Citta))) <> 32
and ascii(substring(Citta,@num,len(Citta))) <> 40
and ascii(substring(Citta,@num,len(Citta))) <> 41
and ascii(substring(Citta,@num,len(Citta))) <> 43
and ascii(substring(Citta,@num,len(Citta))) <> 44
and ascii(substring(Citta,@num,len(Citta))) <> 45
and ascii(substring(Citta,@num,len(Citta))) <> 46
and ascii(substring(Citta,@num,len(Citta))) <> 95
and ascii(substring(Citta,@num,len(Citta))) <> 39
and ascii(substring(Citta,@num,len(Citta))) <> 47
and ascii(substring(Citta,@num,len(Citta))) <> 63
set @num = @num + 1
end
set @num = 1
while @num < 150
begin
insert into #tab_temp
SELECT ascii(substring(Provincia,@num,1)),
substring(Citta,@num,1),
rete,
Cod_Fiscale,
'Provincia',
Provincia
FROM C6Mart.vcontratti
WHERE
ascii(substring(Provincia,@num,len(Provincia))) not between 64 and 90
and ascii(substring(Provincia,@num,len(Provincia))) not between 97 and 122
and ascii(substring(Provincia,@num,len(Provincia))) not between 48 and 57
and ascii(substring(Provincia,@num,len(Provincia))) <> 32
and ascii(substring(Provincia,@num,len(Provincia))) <> 40
and ascii(substring(Provincia,@num,len(Provincia))) <> 41
and ascii(substring(Provincia,@num,len(Provincia))) <> 43
and ascii(substring(Provincia,@num,len(Provincia))) <> 44
and ascii(substring(Provincia,@num,len(Provincia))) <> 45
and ascii(substring(Provincia,@num,len(Provincia))) <> 46
and ascii(substring(Provincia,@num,len(Provincia))) <> 95
and ascii(substring(Provincia,@num,len(Provincia))) <> 39
and ascii(substring(Provincia,@num,len(Provincia))) <> 47
and ascii(substring(Provincia,@num,len(Provincia))) <> 63
set @num = @num + 1
end
set @num = 1
while @num < 150
begin
insert into #tab_temp
SELECT ascii(substring(Nazione,@num,1)),
substring(Nazione,@num,1),
rete,
Cod_Fiscale,
'Nazione',
Nazione
FROM C6Mart.vcontratti
WHERE
ascii(substring(Nazione,@num,len(Nazione))) not between 64 and 90
and ascii(substring(Nazione,@num,len(Nazione))) not between 97 and 122
and ascii(substring(Nazione,@num,len(Nazione))) not between 48 and 57
and ascii(substring(Nazione,@num,len(Nazione))) <> 32
and ascii(substring(Nazione,@num,len(Nazione))) <> 40
and ascii(substring(Nazione,@num,len(Nazione))) <> 41
and ascii(substring(Nazione,@num,len(Nazione))) <> 43
and ascii(substring(Nazione,@num,len(Nazione))) <> 44
and ascii(substring(Nazione,@num,len(Nazione))) <> 45
and ascii(substring(Nazione,@num,len(Nazione))) <> 46
and ascii(substring(Nazione,@num,len(Nazione))) <> 95
and ascii(substring(Nazione,@num,len(Nazione))) <> 39
and ascii(substring(Nazione,@num,len(Nazione))) <> 47
and ascii(substring(Nazione,@num,len(Nazione))) <> 63
set @num = @num + 1
end
set @num = 1
while @num < 150
begin
insert into #tab_temp
SELECT ascii(substring(Presso,@num,1)),
substring(Presso,@num,1),
rete,
Cod_Fiscale,
'Presso',
Presso
FROM C6Mart.vcontratti
WHERE
ascii(substring(Presso,@num,len(Presso))) not between 64 and 90
and ascii(substring(Presso,@num,len(Presso))) not between 97 and 122
and ascii(substring(Presso,@num,len(Presso))) not between 48 and 57
and ascii(substring(Presso,@num,len(Presso))) <> 32
and ascii(substring(Presso,@num,len(Presso))) <> 40
and ascii(substring(Presso,@num,len(Presso))) <> 41
and ascii(substring(Presso,@num,len(Presso))) <> 43
and ascii(substring(Presso,@num,len(Presso))) <> 44
and ascii(substring(Presso,@num,len(Presso))) <> 45
and ascii(substring(Presso,@num,len(Presso))) <> 46
and ascii(substring(Presso,@num,len(Presso))) <> 95
and ascii(substring(Presso,@num,len(Presso))) <> 39
and ascii(substring(Presso,@num,len(Presso))) <> 47
and ascii(substring(Presso,@num,len(Presso))) <> 63
set @num = @num + 1
end
set @num = 1
while @num < 150
begin
insert into #tab_temp
SELECT ascii(substring(flagRol,@num,1)),
substring(flagRol,@num,1),
rete,
Cod_Fiscale,
'flagRol',
flagRol
FROM C6Mart.vcontratti
WHERE
ascii(substring(flagRol,@num,len(flagRol))) not between 64 and 90
and ascii(substring(flagRol,@num,len(flagRol))) not between 97 and 122
and ascii(substring(flagRol,@num,len(flagRol))) not between 48 and 57
and ascii(substring(flagRol,@num,len(flagRol))) <> 32
and ascii(substring(flagRol,@num,len(flagRol))) <> 40
and ascii(substring(flagRol,@num,len(flagRol))) <> 41
and ascii(substring(flagRol,@num,len(flagRol))) <> 43
and ascii(substring(flagRol,@num,len(flagRol))) <> 44
and ascii(substring(flagRol,@num,len(flagRol))) <> 45
and ascii(substring(flagRol,@num,len(flagRol))) <> 46
and ascii(substring(flagRol,@num,len(flagRol))) <> 95
and ascii(substring(flagRol,@num,len(flagRol))) <> 39
and ascii(substring(flagRol,@num,len(flagRol))) <> 47
and ascii(substring(flagRol,@num,len(flagRol))) <> 63
set @num = @num + 1
end
select distinct a.Val_Ascii,a.Val_Char,a.Rete,a.Cod_Fiscale,a.Nome_Campo,a.Val_Campo
into c6mart.TB_ClienticonCaratteriAnomali
from #tab_temp a
inner join c6mart.gestione_pdf_ftp b on a.rete=b.rete and a.cod_fiscale=b.codicefiscale
order by a.cod_fiscale
select * from c6mart.TB_ClienticonCaratteriAnomali order by Cod_Fiscale
select a.Val_Ascii,a.Val_Char,a.Rete,a.Cod_Fiscale,a.Nome_Campo,a.Val_Campo
into c6mart.clientiDaSistemare
from c6mart.TB_ClienticonCaratteriAnomali a
inner join c6mart.gestione_pdf_ftp f on f.rete+f.CodiceFiscale=a.Rete+a.Cod_Fiscale
where f.DataInvio is null
--creare varchar per update
declare @x varchar(max) = 'Val_Campo'
select @x = 'replace('+@x+', ''' + Carattere_Speciale + ''','''+Carattere_Mappato+''')'
from C6Mart.CaratteriMappati
set @x = 'update c6mart.clientidaSistemare set Val_Campo=' + @x +' from c6mart.clientidaSistemare'
exec (@x)
--------------------------
--seconda parte
--------------------------
select * from c6mart.clientiDaSistemare
UPDATE c6mart.clientiDaSistemare SET Val_Campo = replace(Val_Campo, '''', ''); --questo update serve per togliere gli apici che possono dare fastidio negli update
--------------------------
--terza parte
--------------------------
declare @update varchar(max) = '' --ltmp
declare @rete char(1)
declare @cod_fiscale nvarchar(32)
declare @nome_campo nvarchar(64)
declare @val_campo nvarchar(max)
declare ClientiDaSistCurs Cursor for
select rete,cod_fiscale,nome_campo,val_campo from c6mart.clientiDaSistemare
open ClientiDaSistCurs
fetch next from ClientiDaSistCurs into @rete,@cod_fiscale,@nome_campo,@val_campo
while(@@FETCH_STATUS=0)
begin
if(@nome_campo not like '%Cognome%' or @nome_campo not like '%Nome%')
begin
print 'update c6mart.vcontratti
set '+ @nome_campo +'='''+ @val_campo +'''
where cod_fiscale = '''+ @cod_fiscale+''''
--ltmpInizio
set @update = @update + 'update c6mart.vcontratti
set '+ @nome_campo +'='''+ @val_campo +'''
where cod_fiscale = '''+ @cod_fiscale+'\n'''
--ltmpFine
end
else
print 'update c6mart.anag_clienti
set '+ @nome_campo +'='''+ @val_campo +'''
where cod_fiscale = '''+ @cod_fiscale+''''
--ltmpInizio
set @update = @update +
'update c6mart.anag_clienti
set '+ @nome_campo +'='''+ @val_campo +'''
where cod_fiscale = '''+ @cod_fiscale+'\n'''
--ltmpFine
fetch next from ClientiDaSistCurs into @rete,@cod_fiscale,@nome_campo,@val_campo
end
close ClientiDaSistCurs
-- select @update --ltmp
exec (@update) --ltmp
--------------------------
--quarta parte
--------------------------
exec [C6Mart].[InvioLetteraBenvenuto_ExcludedListApply]
--------------------------
--quinta parte
--------------------------
drop table if exists #inviati
select *
into #inviati
from c6mart.gestione_pdf_ftp where codicefiscale in (select distinct riatt_post18.trcom_codfisc--,riatt_post18.trcom_dtinvio
from
(select distinct trcom_codconf,trcom_codfisc,trcom_rete,trcom_dtinvio
from [CONSUNI].ConsulenzaUnicaFL.[dbo].CBS_HistDatiIngresso
where trcom_causale='RCO'
and substring(cast(trcom_dtinvio as varchar),8,4)>2018) revoc_post18 inner join
(select distinct trcom_codconf,trcom_codfisc,trcom_rete,trcom_dtinvio
from [CONSUNI].ConsulenzaUnicaFL.[dbo].CBS_HistDatiIngresso
where trcom_causale='ACO'
and substring(cast(trcom_dtinvio as varchar),8,4)>2018) riatt_post18
on revoc_post18.trcom_codconf<>riatt_post18.trcom_codconf
and revoc_post18.trcom_codfisc=riatt_post18.trcom_codfisc --order by 2 desc
) and datainvio is null order by 2,6
drop table if exists #conta
select CodiceFiscale,count(codicefiscale) as conteggio
into #conta
from C6Mart.GESTIONE_PDF_FTP where CodiceFiscale in (select CodiceFiscale from #inviati)
group by CodiceFiscale
drop table if exists c6mart.clientiRepGiaInviati
select CodiceFiscale,conteggio
into c6mart.clientiRepGiaInviati
from #conta
where conteggio >1
select * from c6mart.clientiRepGiaInviati
--------------------------
--sesta parte
--------------------------
declare @update2 varchar(max) ='' --ltmp
declare @cod_fiscaleINV nvarchar(32)
declare ClientiGiaInviatiCurs Cursor for
select codicefiscale from c6mart.clientiRepGiaInviati
open ClientiGiaInviatiCurs
fetch next from ClientiGiaInviatiCurs into @cod_fiscaleINV
while(@@FETCH_STATUS=0)
begin
print 'update p
set p.[DataInvio] = getdate()
,p.[FlagInvio] = ''S''
,p.[id_fk_zip] = 999999
from [C6Mart].[GESTIONE_PDF_FTP] p where codicefiscale='''+ @cod_fiscaleINV +''' and datainvio is null
'
--ltmpInizio
set @update2 = @update2 +
'update p
set p.[DataInvio] = getdate()
,p.[FlagInvio] = ''S''
,p.[id_fk_zip] = 999999
from [C6Mart].[GESTIONE_PDF_FTP] p where codicefiscale='''+ @cod_fiscaleINV +''' and datainvio is null
'
--ltmpFine
fetch next from ClientiGiaInviatiCurs into @cod_fiscaleINV
end
close ClientiGiaInviatiCurs
-- select @update2 --ltmp
exec (@update2) --ltmp
--fine SP
-------------------------------------------------------
--SELECT * FROM C6MartPeriodico.ContrattoSEI
--WHERE
-- Email like '%¿%'
--OR
-- Indirizzo like '%¿%'
--OR
-- CAP like '%¿%'
--OR
-- Citta like '%¿%'
--OR
-- Prov like '%¿%'
--OR
-- Nazione like '%¿%'
--OR
-- Presso like '%¿%'
--OR
-- Rol like '%¿%'
--OR
-- Email like '%Ç%'
--OR
-- Indirizzo like '%Ç%'
--OR
-- CAP like '%Ç%'
--OR
-- Citta like '%Ç%'
--OR
-- Prov like '%Ç%'
--OR
-- Nazione like '%Ç%'
--OR
-- Presso like '%Ç%'
--OR
-- Rol like '%Ç%'
--OR
-- Email like '%À%'
--OR
-- Indirizzo like '%À%'
--OR
-- CAP like '%À%'
--OR
-- Citta like '%À%'
--OR
-- Prov like '%À%'
--OR
-- Nazione like '%À%'
--OR
-- Presso like '%À%'
--OR
-- Rol like '%À%'
--OR
-- Email like '%É%'
--OR
-- Indirizzo like '%É%'
--OR
-- CAP like '%É%'
--OR
-- Citta like '%É%'
--OR
-- Prov like '%É%'
--OR
-- Nazione like '%É%'
--OR
-- Presso like '%É%'
--OR
-- Rol like '%É%'
--OR
-- Email like '%µ%'
--OR
-- Indirizzo like '%µ%'
--OR
-- CAP like '%µ%'
--OR
-- Citta like '%µ%'
--OR
-- Prov like '%µ%'
--OR
-- Nazione like '%µ%'
--OR
-- Presso like '%µ%'
--OR
-- Rol like '%µ%'
END