/****** Object:  Trigger [TRG_EST_WMS_SAI_RET_LOTE_DUP]    Script Date: 04/02/2015 14:44:49 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_WMS_SAI_RET_LOTE_DUP]'))
DROP TRIGGER [dbo].[TRG_EST_WMS_SAI_RET_LOTE_DUP]
GO
GO
GO
--Versao 3415
CREATE  TRIGGER [dbo].[TRG_EST_WMS_SAI_RET_LOTE_DUP] ON [dbo].[EST_WMS_SAI_RET] 
	FOR INSERT AS
IF EXISTS( SELECT 
					COUNT(S.LOTE) 					 
				FROM
					INSERTED  I
					INNER JOIN EST_WMS_SAI_RET S ON 
					I.CD_PED_WMS	= S.CD_PED_WMS AND 
					I.CD_PROD		= S.CD_PROD  AND 
					I.LOTE			= S.LOTE  AND  
					I.QT_PROD   	= S.QT_PROD
				WHERE 
				S.QT_PROD > 0
				AND (S.LOTE <> 'ND' or S.LOTE <> '0' or isnull(S.LOTE,'') <> '')
				HAVING COUNT(S.LOTE) >1   )
			Begin
				
				RAISERROR ('Lote invalido.',16, 1);
				ROLLBACK TRANSACTION;				
				RETURN;		
			End 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON


GO


