none
ERRO INSERT RRS feed

  • Pergunta

  • Bom Dia Amigos, 

    Necessito de uma ajudinha. Estou executando o insert abaixo  INSERT e o mesmo me retorna o resultado: 

    Msg 2627, Level 14, State 1, Line 2
    Violation of PRIMARY KEY constraint 'PK_ZC_HC'. Cannot insert duplicate key in object 'dbo.ZC_HC'.
    The statement has been terminated.
    Warning: Null value is eliminated by an aggregate or other SET operation.

    INSERT:

    INSERT INTO ZC_HC
    
    
    SELECT 
          PFRATEIOFIXO.CODCOLIGADA AS COLIGADA, 
          PPARAM.ANOCOMP AS ANO,
          PPARAM.MESCOMP AS MES, 
    CASE 
          WHEN PPARAM.MESCOMP=1  THEN 'ENE'
          WHEN PPARAM.MESCOMP=2  THEN 'FEB'
          WHEN PPARAM.MESCOMP=3  THEN 'MAR'
          WHEN PPARAM.MESCOMP=4  THEN 'ABR'
          WHEN PPARAM.MESCOMP=5  THEN 'MAY'
          WHEN PPARAM.MESCOMP=6  THEN 'JUN'
          WHEN PPARAM.MESCOMP=7  THEN 'JUL'
          WHEN PPARAM.MESCOMP=8  THEN 'AGO'
          WHEN PPARAM.MESCOMP=9  THEN 'SEP'
          WHEN PPARAM.MESCOMP=10 THEN 'OCT'
          WHEN PPARAM.MESCOMP=11 THEN 'NOV'
          WHEN PPARAM.MESCOMP=12 THEN 'DIC' END AS MES_EXTENSO,
          SUBSTRING(PFRATEIOFIXO.CODCCUSTO,1,4) AS DIVISAO,
          
    
           COUNT(UD.CHAPA) + COUNT(DEM.CHAPA) - COUNT(ADM.CHAPA)  AS PD,
           COUNT(ADM.CHAPA) AS ADM,      
           COUNT(DEM.CHAPA) AS DEM,
           COUNT(UD.CHAPA)AS UD ,
          PCODNACAO.DESCRICAO AS  NACIONALIDADE
          
    FROM 
    	PFRATEIOFIXO 
    	
    	INNER JOIN PPARAM (NOLOCK) ON
    	PPARAM.CODCOLIGADA = PFRATEIOFIXO.CODCOLIGADA
    	
    	
    	INNER JOIN PFUNC (NOLOCK) ON
    	PFUNC.CODCOLIGADA = PFRATEIOFIXO.CODCOLIGADA AND
    	PFUNC.CHAPA = PFRATEIOFIXO.CHAPA
    	
    	INNER JOIN PPESSOA (NOLOCK) ON
    	PPESSOA.CODIGO=PFUNC.CODPESSOA
    	
    	INNER JOIN PCODNACAO (NOLOCK) ON PCODNACAO.CODCLIENTE=PPESSOA.NACIONALIDADE
    	
    
    	LEFT JOIN PFUNC AS ADM (NOLOCK) ON
    	ADM.CODCOLIGADA = PFRATEIOFIXO.CODCOLIGADA AND
    	ADM.CHAPA = PFRATEIOFIXO.CHAPA AND
    	ADM.CODTIPO NOT IN ('A', 'Z') AND
    	ADM.CODSITUACAO NOT IN ('Z') AND
    	(MONTH(ADM.DATAADMISSAO)=PPARAM.MESCOMP AND
    	YEAR(ADM.DATAADMISSAO)=PPARAM.ANOCOMP) 
    	
    	LEFT JOIN PFUNC AS DEM (NOLOCK) ON
    	DEM.CODCOLIGADA = PFRATEIOFIXO.CODCOLIGADA AND
    	DEM.CHAPA = PFRATEIOFIXO.CHAPA AND
    	DEM.CODTIPO NOT IN ('A', 'Z') AND
    	DEM.CODSITUACAO NOT IN ('Z') AND
    	(MONTH(DEM.DATADEMISSAO)=PPARAM.MESCOMP AND
    	YEAR(DEM.DATADEMISSAO)=PPARAM.ANOCOMP) 
    	
    	LEFT JOIN PFUNC AS UD (NOLOCK) ON
    	UD.CODCOLIGADA = PFRATEIOFIXO.CODCOLIGADA AND
    	UD.CHAPA = PFRATEIOFIXO.CHAPA AND
    	UD.CODTIPO NOT IN ('A', 'Z') AND
    	UD.CODSITUACAO NOT IN ('D','L', 'Z') 
    
     WHERE 	
    	PFRATEIOFIXO.CODCOLIGADA IN ('2','4','6','7') 
    
    GROUP BY
    	PFRATEIOFIXO.CODCOLIGADA,
    	PPARAM.ANOCOMP,
    	PPARAM.MESCOMP,
        SUBSTRING(PFRATEIOFIXO.CODCCUSTO,1,4),
        PCODNACAO.DESCRICAO 
    
    ORDER BY
    	PFRATEIOFIXO.CODCOLIGADA,
    	PPARAM.ANOCOMP,
    	PPARAM.MESCOMP,
        SUBSTRING(PFRATEIOFIXO.CODCCUSTO,1,4)
    

    sábado, 31 de maio de 2014 08:52

Respostas

Todas as Respostas