
/***************************************
* STORED stp_UDC_deleteWithDtx
*
* elimina un dato UDC comprensivo di eventuali Dtx collegati
*
* Steamware, S.E.L.
* mod: 2014.03.08
*
****************************************/
CREATE PROCEDURE [dbo].[stp_UDC_deleteWithDtx]
(
	@UDC	NVARCHAR(50)
)
AS

SET XACT_ABORT ON

BEGIN tran

	-- annullo cella piena
	UPDATE dbo.Celle
	SET Piena = 0
	FROM PosizioneUdcCorrente p
		INNER JOIN dbo.Celle c ON p.IdxCella = c.IdxCella
	WHERE c.Piena = 1 AND p.UDC = @UDC
	
	-- cancello posizione
	DELETE 
	FROM	PosizioneUdcCorrente
	WHERE	UDC = @UDC

	-- toglie associazione DTX in tab origine
	UPDATE dtx
	SET Associato = 0
	FROM dmtx.v_ElencoDatamatrixBMW AS Dtx
	INNER JOIN Dtx2UDC d2c ON Dtx.DataMatrix COLLATE SQL_Latin1_General_CP1_CI_AS = d2c.DataMatrix
	WHERE d2c.UDC = @UDC
		AND dtx.Associato = 1
		
	-- Dtx2UDC
	DELETE
	FROM	Dtx2UDC
	WHERE	UDC = @UDC

	-- cancello UDC
	DELETE
	FROM	ElencoCartellini
	WHERE	UDC = @UDC

COMMIT tran

RETURN