SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Triggers
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aidmondo
Starting Member

Ghana
23 Posts

Posted - 05/25/2012 :  06:52:49  Show Profile  Reply with Quote
Create table PaymentMethods(
PaymentMethodID int Primary Key,
Description not null
)
The Descriotion is either cheque, cash or Credit Card.

Create table Payments(
PaymentID int Primary Key,
PaymentMenthodID int Constraint cfkPay Foreign Key References (#above) not null,
ChequeNo varchar(20) not null
)

Now my question is if the description is cheque, the ChequeNo should be entered and if not it should be left blank. How?

Edited by - aidmondo on 05/25/2012 08:02:40

visakh16
Very Important crosS Applying yaK Herder

India
47036 Posts

Posted - 05/25/2012 :  09:40:41  Show Profile  Reply with Quote
you can use a check constraint based on UDF like below

CREATE FUNCTION dbo.GetPaymentStatus
(
@PaymentMethodID int,
@ChequeNo varchar(20)
)
RETURNS bit
AS
BEGIN 
DECLARE @Ret bit

SELECT @Ret=CASE WHEN Description ='cheque' AND @ChequeNo IS NULL THEN 0
ELSE 1 END
FROM PaymentMethods
WHERE PaymentMethodID = @PaymentMethodID 

RETURN(@Ret)
END


then create check constraint based on it
ALTER TABLE Payments ADD CONSTRAINT Chk_PaymentMethod CHECK (dbo.GetPaymentStatus(PaymentMenthodID,ChequeNo)=1)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3425 Posts

Posted - 05/25/2012 :  09:47:21  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
Sparse columns aren't really a good fit for relational databases.

you could make the ChequeNo NULL'able and insert a null... If you end up with a bunch of these guys you are going to be wasting a lot of space for no benefit.

or, perhaps cleaner you could have a PatmentDetails table with a foreign key back to pyments and then insert rows into there for the details.

Transact Charlie
Msg 3903, Level 16, State 1, Line 1736:
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION

http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

aidmondo
Starting Member

Ghana
23 Posts

Posted - 06/02/2012 :  07:48:00  Show Profile  Reply with Quote
When I execute the function, i get the error message 'cannot insert null into the column ChequeNo and if i allow ChequeNo accepts null values, the ChequeNo column accept all values even if description in not Cheque.


aidmondo

Edited by - aidmondo on 06/02/2012 07:58:42
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47036 Posts

Posted - 06/02/2012 :  13:44:18  Show Profile  Reply with Quote
quote:
Originally posted by aidmondo

When I execute the function, i get the error message 'cannot insert null into the column ChequeNo and if i allow ChequeNo accepts null values, the ChequeNo column accept all values even if description in not Cheque.


aidmondo


do you mean while creating check constraint?

I'm not doing any insert to check field inside function so didnt understand what you mean by getting an error while creating function.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

aidmondo
Starting Member

Ghana
23 Posts

Posted - 06/07/2012 :  14:03:13  Show Profile  Reply with Quote
After the function have been executed and table altered, the chequeno column in the Payments table still accepts values even if the PaymentMethod Description is cash or creditcard.

aidmondo
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3821 Posts

Posted - 06/07/2012 :  14:35:18  Show Profile  Reply with Quote
I'm not sure if this is any better or more correc that Visakh's solution (as I didn't try it). but, maybe it'l help?

-- Create Tables

CREATE TABLE PaymentMethod
(
	PaymentMethodID INT IDENTITY(1, 1) PRIMARY KEY,
	Description VARCHAR(20) NOT NULL
)


CREATE TABLE Payment
(
	PaymentID INT IDENTITY(1, 1) PRIMARY KEY,
	PaymentMenthodID INT NOT NULL CONSTRAINT cfkPay FOREIGN KEY (PaymentMenthodID) REFERENCES PaymentMethod (PaymentMethodID),
	ChequeNo varchar(20) not null 
)

ALTER TABLE Payment
ADD CONSTRAINT CK_Payment_ChequeNo CHECK ((CASE WHEN PaymentMenthodID = 1 AND LEN(ChequeNo) > 0 THEN 1 WHEN PaymentMenthodID <> 1 AND LEN(ChequeNo) = 0 THEN 1 ELSE 0 END) = 1)

-- Popualte Domain Data

SET IDENTITY_INSERT PaymentMethod ON
INSERT PaymentMethod (PaymentMethodID, Description)
VALUES
(1, 'Cheque'),
(2, 'Cash'),
(3, 'Credit Card')
SET IDENTITY_INSERT PaymentMethod ON


-- Try Inserting

INSERT Payment (PaymentMenthodID, ChequeNo) VALUES (1, NULL) -- Fail
INSERT Payment (PaymentMenthodID, ChequeNo) VALUES (1, '') -- Fail
INSERT Payment (PaymentMenthodID, ChequeNo) VALUES (1, '12345') -- Success
INSERT Payment (PaymentMenthodID, ChequeNo) VALUES (2, '12345') -- Fail
INSERT Payment (PaymentMenthodID, ChequeNo) VALUES (2, '') -- Success

-- Drop tables
DROP TABLE Payment
DROP TABLE PaymentMethod
EDIT: Added DROP

Edited by - Lamprey on 06/07/2012 14:45:30
Go to Top of Page

aidmondo
Starting Member

Ghana
23 Posts

Posted - 06/07/2012 :  18:15:07  Show Profile  Reply with Quote
Thanks a lot for the help

aidmondo
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000