Home Forums SQL Server 2008 SQL Server 2008 - General Check Constraint Question - Situations Where a Child Record Should Be Limited to One Occurrance RE: Check Constraint Question - Situations Where a Child Record Should Be Limited to One Occurrance

  • yes, you can make a check constraint that uses a user defined function.

    something like this is untested, but kind of gives you an idea of how it would work

    create function dbo.LimitedToOne (@GroupId int)

    returns int

    as

    begin

    return (select count(ANumber) from dbo.TestTable where GroupId=@GroupId)

    end

    GO

    alter table dbo.TestTable add constraint chkgrp check (dbo.LimitedToOne(GroupId) <= 1)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!