Declare @EmpresaBase Int = 3 /*Empresa que será usada como base para os registros ŕ serem inseridos*/ declare @EmpresaNova Int =4 /*Empresa nova que será inserida e referenciada nos novos registros*/ declare @cmd nvarchar(max) /*Clonar Financeiro de Pessoa*/ set @cmd = ' insert into FN_PES_FIN (ID_PES_FIN, ID_EMP, ' + dbo.tempFunc_GetColunasTabela('FN_PES_FIN', '', '') + ')' + ' select next value for G_ID_PES_FIN, '+cast(@EmpresaNova as varchar(10))+', ' + dbo.tempFunc_GetColunasTabela('FN_PES_FIN', '', '') + ' from FN_PES_FIN PC WHERE PC.ID_EMP = '+cast(@EmpresaBase as varchar(10))+' and not exists (select 1 from FN_PES_FIN pc2 where pc2.ID_PES = PC.ID_PES and pc2.ID_EMP = '+cast(@EmpresaNova as varchar(10))+')' exec SP_EXECUTESQL @cmd;