Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help

Author  Topic 

sanlen
Starting Member

29 Posts

Posted - 2008-11-21 : 20:55:46
Hi All,

I have table as below:

create table table_payment
(payment_method VARCHAR(30), credit_card_no VARCHAR(30),
card_holder_name VARCHAR(30), card_expired_date DATETIME)

Every time i start to enter data, I want to check that if the payment_method is 'Credit Card' then the other 3 fields cannot be null, else it can be null.

Could you please advise?

Thanks you very much for your time

Best Regard,
SANLEN

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-21 : 23:56:50
use a INSTEAD OF INSERT trigger for this

CREATE TRIGGER CheckData ON table_payment
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS(SELECT 1
FROM INSERTED
WHERE payment_method='Credit Card'
AND (credit_card_no IS NULL
OR card_holder_name IS NULL
OR card_expired_date IS NULL))
RAISERROR ('Credit payments should have not null values for card details',10,1
ELSE
INSERT INTO table_payment
SELECT payment_method,credit_card_no,
card_holder_name,card_expired_date
FROM INSERTED
END
Go to Top of Page
   

- Advertisement -