

/*************************************************
* STORED stp_getFirstLastDtx
*
* fornisce primo-ultimo DataMatrix della posizione/cella indicata
*
* Steamware, S.E.L.
* mod: 2014.03.12
*
*************************************************/
CREATE PROCEDURE [dmtx].[stp_getFirstLastDtx]
(
	@CodCella	NVARCHAR(50)
)
AS

DECLARE	@DtxStart	NVARCHAR(50),
		@DtxEnd		NVARCHAR(50)


-- recupero primo DataMatrix
SELECT  TOP 1 @DtxStart = d2c.DataMatrix
FROM	dmtx.v_ElencoDatamatrixBMW edb
INNER JOIN dbo.Dtx2UDC d2c
	ON	edb.DataMatrix = d2c.DataMatrix COLLATE Latin1_General_CI_AS
INNER JOIN dbo.PosizioneUdcCorrente puc
	ON	d2c.UDC = puc.UDC
INNER JOIN Celle c
	ON puc.IdxCella = c.IdxCella
WHERE	c.CodCella = @CodCella
	AND edb.EsitoMarcatura = 'OK'
ORDER BY edb.DataOra

-- recupero ultimo DataMatrix
SELECT  TOP 1 @DtxEnd = d2c.DataMatrix
FROM	dmtx.v_ElencoDatamatrixBMW edb
INNER JOIN dbo.Dtx2UDC d2c
	ON	edb.DataMatrix = d2c.DataMatrix COLLATE Latin1_General_CI_AS
INNER JOIN dbo.PosizioneUdcCorrente puc
	ON	d2c.UDC = puc.UDC
INNER JOIN Celle c
	ON puc.IdxCella = c.IdxCella
WHERE	c.CodCella = @CodCella
	AND edb.EsitoMarcatura = 'OK'
ORDER BY edb.DataOra DESC

-- restituisco output
SELECT @DtxStart AS DtxStart, @DtxEnd AS DtxEnd

RETURN