DECLARE @sql NVARCHAR(MAX) = N'INSERT INTO vida (iditem, plano, obs) SELECT i.iditem, i.descricao, r.obs FROM item i INNER JOIN documento d on d.iddocumento = i.iddocumento INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN riscosdiversos r on r.iditem = i.iditem WHERE c.idramo = 47;'

DECLARE @sqlDelete NVARCHAR(MAX) = N'DELETE FROM riscosdiversos WHERE iditem in (SELECT i.iditem FROM item i INNER JOIN documento d on d.iddocumento = i.iddocumento INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN riscosdiversos r on r.iditem = i.iditem WHERE c.idramo = 47);'

IF EXISTS(SELECT 1 FROM item i INNER JOIN documento d on d.iddocumento = i.iddocumento INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN riscosdiversos r on r.iditem = i.iditem WHERE c.idramo = 47)
BEGIN
    exec sp_executesql @sql
    exec sp_executesql @sqlDelete
END