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
 Help with Date

Author  Topic 

aidmondo
Starting Member

23 Posts

Posted - 2012-05-14 : 09:57:18
I have a table Events with (EventID int Primary Key Identity,
StartDate datetime not null)
Now I have to Create table Payment(PaymentID int Primary Key,
EventID int Foreign Key References Events(EventID),
PaymentDate datetime)
how do I Specify that PaymentDate should be < StartDate in events table.

aidmondo

dipuvp2004
Starting Member

1 Post

Posted - 2012-05-20 : 15:59:24
This is possible using a triggger. for insert and update validation can be done inside the trigger.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-20 : 17:30:07
CHECK constraint based on UDF should be enough. something like

CREATE FUNCTION DateValidate
(
@Date datetime,
@EventID int
)
RETURNS bit
AS
BEGIN
DECLARE @Ret bit,@StartDate datetime

SELECT @StartDate = StartDate
FROM Events
WHERE EventID = @EventID

SELECT @Ret = CASE WHEN @Date > @StartDate THEN 0 ELSE 1 END

RETURN (@Ret)
END



ALTER TABLE Payment ADD CONSTRAINT Chk_PaymentDate CHECK (dbo.DateValidate(PaymentDate,EventID) = 1)


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

Go to Top of Page
   

- Advertisement -