DECLARE @SQL NVARCHAR ( MAX ) = N'INSERT INTO auto (iditem, obs) SELECT i.iditem, 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 = 37;' 
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 = 37);'
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 = 37 
	) BEGIN
	EXEC sp_executesql @SQL EXEC sp_executesql @sqlDelete 
END