IF NOT EXISTS (SELECT * FROM SYS.COLUMNS WHERE NAME = 'CD_CTR_ENVIO' AND OBJECT_ID = OBJECT_ID('PG_DEB_PAGFOR_CONTROLE'))
begin
ALTER TABLE dbo.PG_DEB_PAGFOR_CONTROLE ADD
	CD_CTR_ENVIO int NOT NULL CONSTRAINT DF_PG_DEB_PAGFOR_CONTROLE_CD_CTR_ENVIO DEFAULT 0

CREATE TABLE #RSDUP (CD_EMP INT,CD_FILIAL INT, CD_PG_CRED INT, CD_CTR INT)

INSERT INTO #RSDUP
SELECT 
	CD_EMP,
	CD_FILIAL,
	CD_PG_CRED,
	CONVERT(VARCHAR(10),ROW_NUMBER() OVER (PARTITION BY CD_EMP ORDER BY CD_EMP)) AS CTR
FROM
	PG_DEB_PAGFOR_CONTROLE

UPDATE
	PG_DEB_PAGFOR_CONTROLE
SET
	PG_DEB_PAGFOR_CONTROLE.CD_CTR_ENVIO = R.CD_CTR
FROM
	PG_DEB_PAGFOR_CONTROLE INNER JOIN #RSDUP R ON
	PG_DEB_PAGFOR_CONTROLE.CD_EMP = R.CD_EMP AND
	PG_DEB_PAGFOR_CONTROLE.CD_FILIAL = R.CD_FILIAL AND
	PG_DEB_PAGFOR_CONTROLE.CD_PG_CRED = R.CD_PG_CRED
	

DROP TABLE #RSDUP


end