DECLARE @CD_EMP INT
DECLARE @CD_CLI INT
DECLARE @CD_PLANO_VENDA INT

DECLARE db_cursor CURSOR FOR  

SELECT DISTINCT
	A.CD_EMP,
	A.CD_CLI,
	A.CD_PLANO_VENDA
FROM RC_CLI A INNER JOIN RC_CLI_CONV_RC_CLI B ON
	A.CD_EMP=B.CD_EMP AND
	A.CD_CLI=B.CD_CLI
WHERE B.CD_PLANO_VENDA = 0
and A.CD_PLANO_VENDA >0
AND A.CD_EMP=1


OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO 	@CD_EMP,@CD_CLI,@CD_PLANO_VENDA

WHILE @@FETCH_STATUS = 0   
BEGIN   

	   UPDATE RC_CLI_CONV_RC_CLI  SET  CD_PLANO_VENDA=@CD_PLANO_VENDA
	          WHERE CD_EMP = @CD_EMP AND CD_CLI=@CD_CLI

       FETCH NEXT FROM db_cursor INTO @CD_EMP,@CD_CLI,@CD_PLANO_VENDA
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

