I have the following SQL:
INSERT INTO SISCli (CdInscricao,DsEntidade,DsApelido,InInscricao,InCadastro,DsEndereco,NrCEP,NrInscricaoEstadual,NrCGCCPF,NrTelefone,DtCadastro,CdEmpresa,DsEMail,DsUSuarioInc,InClassificacaoFiscal,DsBairro)
SELECT
case when LEN(ESP353_XML.DES_NrCGCCPF)=13 then '0'+ CAST(ESP353_XML.DES_NrCGCCPF AS VARCHAR)
when LEN(ESP353_XML.DES_NrCGCCPF)=12 then '00'+ CAST(ESP353_XML.DES_NrCGCCPF AS VARCHAR)
when LEN(ESP353_XML.DES_NrCGCCPF)=11 then '000'+ CAST(ESP353_XML.DES_NrCGCCPF AS VARCHAR)
when LEN(ESP353_XML.DES_NrCGCCPF)=10 then '0000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=9 then '00000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=8 then '000000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=7 then '0000000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=6 then '00000000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
else CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
end AS [CdInscricao],
ESP353_XML.DES_DsEntidade AS [DsEntidade],
ESP353_XML.DES_DsApelido AS [DsApelido],
case when LEN(ESP353_XML.DES_NrCGCCPF)>11 then 0 else 1 end AS [InInscricao],
0 AS [InCadastro],
ESP353_XML.DES_DsEndereco AS [DsEndereco],
ESP353_XML.DES_NrCEP AS [NrCEP],
'ISENTO' AS [NrInscricaoEstadual],
case when LEN(ESP353_XML.DES_NrCGCCPF)=13 then '0'+ CAST(ESP353_XML.DES_NrCGCCPF AS VARCHAR)
when LEN(ESP353_XML.DES_NrCGCCPF)=12 then '00'+ CAST(ESP353_XML.DES_NrCGCCPF AS VARCHAR)
when LEN(ESP353_XML.DES_NrCGCCPF)=11 then ''+ CAST(ESP353_XML.DES_NrCGCCPF AS VARCHAR)
when LEN(ESP353_XML.DES_NrCGCCPF)=10 then '0'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=9 then '00'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=8 then '000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=7 then '0000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
when LEN(ESP353_XML.DES_NrCGCCPF)=6 then '00000'+ CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
else CAST(ESP353_XML.DES_NrCGCCPF AS varchar)
end AS [NrCGCCPF],
ESP353_XML.DES_NrTelefone AS [NrTelefone],
GETDATE() AS [DtCadastro],
1 as [CdEmpresa],
'[email protected]' as [DsEMail],
'EdiXMLMI' AS [DsUSuarioInc],
7 AS [InClassificacaoFiscal],
substring(ISNULL(ESP353_XML.DES_DsBairro,0),1,15) as [DES_DsBairro]
FROM ESP353_XML
where NOT exists (select 1 from SISCli A WHERE cast(A.CdInscricao as numeric) = cast(ESP353_XML.DES_NrCGCCPF as numeric))
Running it returns the error below:
Mensagem 512, Nível 16, Estado 1, Procedimento TRG_BloqueioInsereRegInvalido, Linha 7
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
But if I put a top 1 after SELECT
and go running one by one it works.
So far I have not found the error that returns below and why.