/****** Object:  Trigger [TRG_RC_CREDIARIO_PARC_DT_FECHA_VENCTO]    Script Date: 08/16/2012 09:47:10 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_RC_CREDIARIO_PARC_DT_FECHA_VENCTO]'))
DROP TRIGGER [dbo].[TRG_RC_CREDIARIO_PARC_DT_FECHA_VENCTO]
GO

/****** Object:  Trigger [dbo].[TRG_RC_CREDIARIO_PARC_DT_FECHA_VENCTO]    Script Date: 04/03/2015 14:12:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[TRG_RC_CREDIARIO_PARC_DT_FECHA_VENCTO] ON [dbo].[RC_CREDIARIO_PARC]
   FOR INSERT, UPDATE 
AS 
BEGIN
	
	declare @Rs  table (cd_Ctr int, dtfecha datetime, dtVencto datetime)

	IF UPDATE(DT_LANC_CRED)
		BEGIN
			insert into @Rs
			select 
				i.cd_ctr,
				dbo.F_DIA_PREV_FECHA_VENC_CRED(i.CD_EMP, i.CD_CLI, i.DT_LANC_CRED, i.PARC_CRED,0),
				dbo.F_DIA_PREV_FECHA_VENC_CRED(i.CD_EMP, i.CD_CLI, i.DT_LANC_CRED, i.PARC_CRED,1)
			FROM 
				INSERTED i 

			delete from r
			from
				@Rs r
			where
				dtfecha is null or
				dtVencto is null;  

			UPDATE p SET 
				DT_FECHA_CRED = dtfecha,
				DT_VENCTO_CRED = dtVencto
			FROM 
				RC_CREDIARIO_PARC p 
				INNER JOIN @Rs r ON
				p.CD_CTR = r.CD_CTR
		END

END


GO


