Return to Snippet

Revision: 16098
at July 27, 2009 01:17 by rengber


Updated Code
ALTER TABLE [dbo].[Message]  WITH CHECK ADD  CONSTRAINT [CK_Message] CHECK  (([InReplyToId] IS NULL OR [dbo].[CheckInReplyToMessageId]([InReplyToId])>=(0)))
GO
ALTER TABLE [dbo].[Message] CHECK CONSTRAINT [CK_Message]



CREATE FUNCTION [dbo].[CheckInReplyToMessageId] 
(
    @MessageId BigInt
)
RETURNS bit
AS
BEGIN
    DECLARE @RetVal bit

    SET @RetVal = (SELECT MessageId
                     FROM Message
                     WHERE MessageId = @MessageId)

    RETURN isnull(@RetVal, 0)

END

Revision: 16097
at July 26, 2009 23:57 by rengber


Initial Code
[InReplyToId]  IS NULL 
OR
([InReplyToId] IN
  (SELECT     MessageId
   FROM          Message
   WHERE      (MessageId = [InReplyToId])
  )
)

Initial URL


Initial Description
If a "InReplyToID" is provided, it must be a valid MessageId

Initial Title
TSQL Function and Check Constraint to Ensure that a Parent Value is Present in the Table

Initial Tags
database, sql, data

Initial Language
SQL