Trigger para evitar campos NULL en una tabla
Recién estoy incursionando al SQL Server y necesito crear un Trigger para evitar que dejen en blanco (NULL) 3 campos de 6 en una tabla y no me funciona:
CREATE TRIGGER trgpagocontarjeta
ON Payments
FOR INSERT
AS
--DECLARE @vPaymentMethodDesc varchar(20)
DECLARE @PaymentMethod_Id char(10)
DECLARE @CreditCardNo char(15)
DECLARE @CardHolderName varchar(15)
DECLARE @ExiryDate datetime
--SELECT @vPaymentMethodDesc = vPaymentMethodDesc FROM PaymentMethods JOIN INSERTED ON PaymentMethods.PaymentMethod_Id=INSERTED.PaymentMethod_Id
SELECT @PaymentMethod_Id = PaymentMethod_Id FROM INSERTED
--IF (@vPaymentMethodDesc = 'PM001')
IF (@PaymentMethod_Id = 'PM001')
-- PM001 es el ID del pago con Tarjeta de Crédito
BEGIN
SELECT @CreditCardNo=CreditCardNo FROM INSERTED
SELECT @CardHolderName=CardHolderName FROM INSERTED
SELECT @ExiryDate=ExiryDate FROM INSERTED
IF ((@CreditCardNo=NULL) OR (@CardHolderName=NULL) OR (@ExiryDate=NULL))
BEGIN
PRINT 'Necesitas llenar los campos de la tarjeta de credito'
ROLLBACK TRANSACTION
END
END
CREATE TRIGGER trgpagocontarjeta
ON Payments
FOR INSERT
AS
--DECLARE @vPaymentMethodDesc varchar(20)
DECLARE @PaymentMethod_Id char(10)
DECLARE @CreditCardNo char(15)
DECLARE @CardHolderName varchar(15)
DECLARE @ExiryDate datetime
--SELECT @vPaymentMethodDesc = vPaymentMethodDesc FROM PaymentMethods JOIN INSERTED ON PaymentMethods.PaymentMethod_Id=INSERTED.PaymentMethod_Id
SELECT @PaymentMethod_Id = PaymentMethod_Id FROM INSERTED
--IF (@vPaymentMethodDesc = 'PM001')
IF (@PaymentMethod_Id = 'PM001')
-- PM001 es el ID del pago con Tarjeta de Crédito
BEGIN
SELECT @CreditCardNo=CreditCardNo FROM INSERTED
SELECT @CardHolderName=CardHolderName FROM INSERTED
SELECT @ExiryDate=ExiryDate FROM INSERTED
IF ((@CreditCardNo=NULL) OR (@CardHolderName=NULL) OR (@ExiryDate=NULL))
BEGIN
PRINT 'Necesitas llenar los campos de la tarjeta de credito'
ROLLBACK TRANSACTION
END
END
1 respuesta
Respuesta de morenesi
1