Create PROCEDURE [dbo].[sp_PLAY_Cancella_Riferimento_FRContabilizzazioni]
	-- Add the parameters for the stored procedure here
	@p_IDFRContabilizzazione	int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
    DECLARE @EsistePLAY				bit
    DECLARE @NomeCompletoDBPlay		nvarchar(MAX)
    
	IF NOT EXISTS 
	(SELECT * 
		FROM sys.objects 
			WHERE object_id = OBJECT_ID(N'[dbo].ZZZ_Play_Parametri') 
				AND type in (N'U')
	)
		BEGIN
			SET @EsistePLAY=0
		END
	ELSE
		BEGIN
			 SELECT	@EsistePLAY=isnull(P.EsistePlay, 0)
		,			@NomeCompletoDBPlay=isnull(P.NomeCompletoDBPlay, '')
			FROM	ZZZ_Play_Parametri	AS	P		
		END            
 
    IF @EsistePLAY=1
		BEGIN
    
			DECLARE @Comando			nvarchar(MAX)
			SET @Comando =	'DELETE ' + @NomeCompletoDBPlay + '.dbo.FRContabilizzazioni ' +
							'WHERE IDFRContabilizzazione=' + @p_IDFRContabilizzazione
			EXEC(@Comando)
	
		END
	
END
