| Author |
Topic |
|
aidmondo
Starting Member
23 Posts |
Posted - 2012-05-13 : 06:51:04
|
| Create table PaymentMethods(PaymentMethodID int Primary Key,Description not null)Create table Payments(PaymentID int Primary Key,PaymentMenthodID int Constraint cfkPay Foreign Key References (#above) not null,ChequeNo int not null)Now my question is ChequeNo should be entered if the description is cheque and if not it should be left blank. How?aidmondo |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-13 : 06:59:07
|
| [code]First of all this should be your tables design Create table PaymentMethods(PaymentMethodID int Primary Key,Description varchar(200)not null)Create table Payments(PaymentID int Primary Key,PaymentMenthodID int Constraint cfkPay References PaymentMethods (PaymentMethodID) not null,ChequeNo int not null)[/code]Vijay is here to learn something from you guys. |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-13 : 07:03:25
|
| [code]What value do you want to insert into chequeno column.Can you please provide some sample data.[/code]Vijay is here to learn something from you guys. |
 |
|
|
aidmondo
Starting Member
23 Posts |
Posted - 2012-05-13 : 12:39:15
|
| If Description is Cheque, ChequeNo is 2001100547701If Description is Cash, ChequeNo is Null |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-13 : 13:51:48
|
quote: Originally posted by aidmondo If Description is Cheque, ChequeNo is 2001100547701If Description is Cash, ChequeNo is Null
Try below code with table design what I have created .And make sure there are only two option should be either cheque or cash ...if apart from cash yo insert demand draft then it will also insert NULL value.Create table PaymentMethods(PaymentMethodID int Primary Key,Description varchar(200)not null)Create table Payments(PaymentID int Primary Key identity (1,1),PaymentMenthodID int Constraint cfkPay References PaymentMethods (PaymentMethodID) not null,ChequeNo varchar(30) )create trigger trg on PaymentMethodsafter insert asSET NOCOUNT ON declare @Description varchar(20) declare @PaymentMethodID varchar(20) select @Description= i.Description from inserted i select @PaymentMethodID = i.PaymentMethodID from inserted i if @Description = 'CHecque' insert into Payments (PaymentMenthodID,ChequeNo)values( @PaymentMethodID,'2001100547701') else insert into Payments (PaymentMenthodID,ChequeNo)values(@PaymentMethodID,null) insert into PaymentMethods values (1,'CHecque')select * from Paymentsinsert into PaymentMethods values(2,'cash')select * from Payments Vijay is here to learn something from you guys. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-13 : 14:02:10
|
The best way is implement it by means of UDF and use it in CHECK constraintCREATE FUNCTION dbo.ChequeNoValidate(@ChequeNo varchar(30),@PaymentMethodID int)RETURNS BitASBEGINDECLARE @Ret bit,@PaymentMethod varchar(200)SET @Ret =1SELECT @PaymentMethod = DescriptionFROM PaymentMethodsWHERE PaymentMethodID = @PaymentMethodIDIF (@PaymentMethod = 'Cheque' AND @ChequeNo IS NULL)OR (@PaymentMethod = 'Cash' AND @ChequeNo IS NOT NULL)SELECT @Ret = 0 RETURN (@Ret)ENDCreate table PaymentMethods(PaymentMethodID int Primary Key,Description varchar(200)not null)Create table Payments(PaymentID int Primary Key identity (1,1),PaymentMenthodID int Constraint cfkPay References PaymentMethods (PaymentMethodID) not null,ChequeNo varchar(30) not null)ALTER TABLE PaymentsADD CONSTRAINT chkCheckNo CHECK (dbo.ChequeNoValidate(ChequeNo,@PaymentMethodID) = 1 ); ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-13 : 14:03:06
|
quote: Originally posted by vijays3
quote: Originally posted by aidmondo If Description is Cheque, ChequeNo is 2001100547701If Description is Cash, ChequeNo is Null
Try below code with table design what I have created .And make sure there are only two option should be either cheque or cash ...if apart from cash yo insert demand draft then it will also insert NULL value.Create table PaymentMethods(PaymentMethodID int Primary Key,Description varchar(200)not null)Create table Payments(PaymentID int Primary Key identity (1,1),PaymentMenthodID int Constraint cfkPay References PaymentMethods (PaymentMethodID) not null,ChequeNo varchar(30) )create trigger trg on PaymentMethodsafter insert asSET NOCOUNT ON declare @Description varchar(20) declare @PaymentMethodID varchar(20) select @Description= i.Description from inserted i select @PaymentMethodID = i.PaymentMethodID from inserted i if @Description = 'CHecque' insert into Payments (PaymentMenthodID,ChequeNo)values( @PaymentMethodID,'2001100547701') else insert into Payments (PaymentMenthodID,ChequeNo)values(@PaymentMethodID,null) insert into PaymentMethods values (1,'CHecque')select * from Paymentsinsert into PaymentMethods values(2,'cash')select * from Payments Vijay is here to learn something from you guys.
In this trigger you're assuming only single rows inserts happen.what if it was a batch insertion?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-13 : 14:05:42
|
| i was assuming single row insertion ...Thanks for your Define functionVijay is here to learn something from you guys. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-13 : 14:25:56
|
quote: Originally posted by vijays3 i was assuming single row insertion ...Thanks for your Define functionVijay is here to learn something from you guys.
No problem you're welcomeYou can achieve it using INSTEAD OF INSERT trigger also------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|