Ver Mensaje Individual
  #9  
Antiguo 12-02-2010
Avatar de ContraVeneno
ContraVeneno ContraVeneno is offline
Miembro
 
Registrado: may 2005
Ubicación: Torreón, México
Posts: 4.738
Reputación: 23
ContraVeneno Va por buen camino
ya, muy válido tu comentario... así que con algunos cambios y unos retoques aquí y allá, me queda lo siguiente, que me funciona a la perfección:

Código SQL [-]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[triRevisaStatusTipoCambio]    
   ON [dbo].[TipoCambioDep]
   INSTEAD OF INSERT
AS 
BEGIN
   -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    Declare @Valor int
    Declare @NewStatus Bit
    
    Set @Valor = 0
    
    Select @NewStatus = Status
    From Inserted
    
    Select @Valor = IsNull(Count(*), 0)
    From TipoCambioDep T
    Inner Join Inserted I on T.Departamento = I.Departamento 
    Where T.Status <> 0
    
    If (@Valor >= 1) and (@NewStatus <> 0) Begin    
      Update TipoCambioDep
      Set Status = 0
      From TipoCambioDep T
      Inner Join Inserted I on T.Departamento = I.Departamento 
      Where T.Status <> 0      
    end
    
    Insert into TipoCambioDep(Departamento, TipoCambio, Status)
    Select Departamento, TipoCambio, Status
    From Inserted 

END
-------------------UPDATE -------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER truRevisaStatusTipoCambio    
   ON TipoCambioDep
   INSTEAD OF UPDATE   
AS 
BEGIN
   -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    
    DECLARE @OldID INT
    DECLARE @NewID INT
    Declare @NewStatus Bit
 
    SELECT @OldID = Departamento FROM DELETED
    
    Select @NewID = I.Departamento, @NewStatus = I.Status
    From Inserted I    
    
    Declare @Valor int
    
    Set @Valor = 0
        
    Select @Valor = IsNull(Count(*), 0)
    From TipoCambioDep T
    Inner Join Inserted I on T.Departamento = I.Departamento 
    Where T.Status <> 0
    
    If (@Valor >= 1) and (@NewStatus <> 0) Begin
      RAISERROR ('Ya existe un registro activo para esta tabla', 16, 1)
      ROLLBACK TRANSACTION
      Return
    end
    
    If (@Valor = 1) and (@NewStatus = 0) Begin
      RAISERROR ('Debe existir al menos un registro activo.', 16, 1)
      ROLLBACK TRANSACTION
      Return
    end
    
    Update TipoCambioDep
    Set Departamento = I.Departamento, TipoCambio = I.TipoCambio, Status = I.Status
    From TipoCambioDep T
    inner Join Inserted I on I.Departamento = @NewID
    Where T.Departamento = @OldID
END
GO

Faltaría validar la parte del "DELETE", pero eso lo haremos en la próxima versión .

Nuevamente, a menos que alguien tenga algo que aportar o algo que señalar, quedaría resuelto.

Gracias a todos.
__________________

Responder Con Cita