1

I have three tables a,b and c and need to add a constraint like below for checking the data integrity

The below is wrong but I need help in enforcing the below condition.

ALTER TABLE [a]
ADD CONSTRAINT  UOMGROUPIG CHECK UNITOFMEASURID IN (SELECT UnitOfMeasureId FROM b  WHERE UOMGroupId=1 )

ALTER TABLE [c]
ADD CONSTRAINT  UOMGROUPIG CHECK UNITOFMEASURID IN (SELECT UnitOfMeasureId FROM b  WHERE UOMGroupId=2 )

Thanks

3
  • Thanks I am actually checking for the records available there (SELECT UnitOfMeasureId FROM b WHERE UOMGroupId=2 ) this could return as 4 , 78,43,1 so the column UOMGROUPIG should allow these values alone. Commented Nov 28, 2012 at 9:41
  • Can't you use a foreign key for that? Commented Nov 28, 2012 at 10:30
  • @a_horse_with_no_name There is WHERE UOMGroupId=1 condition, I believe this eliminates the use of a foreign key. Commented Nov 28, 2012 at 11:16

2 Answers 2

1

A scalar valued function like this works for your example, you can easily modify it and create your second constraint:

CREATE FUNCTION your_schema_name.udf_Check1(
    @UNITOFMEASURID INT
)
RETURNS BIT
AS 
BEGIN

    DECLARE @returnValue BIT = 0

    SELECT  @returnValue = CASE WHEN COUNT(UnitOfMeasureId) > 0 THEN 1 ELSE 0 END
    FROM    your_schema_name.b 
    WHERE   UOMGroupId=1
    AND     @UNITOFMEASURID = UnitOfMeasureId

    RETURN @returnValue

END
GO

ALTER TABLE [a] 
ADD CONSTRAINT UOMGROUPIG 
CHECK (your_schema_name.udf_Check1(UNITOFMEASURID) = 1)
GO  

Here is the example: SQL Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

@ivanThanks I am actually checking for the records available there (SELECT UnitOfMeasureId FROM b WHERE UOMGroupId=2 ) this could return as 4 , 78,43,1 so the column UOMGROUPIG should allow these values alone.
You're welcome. I understood your check constraint as if you wanted any value that was getting inserted into a.UNITOFMEASURID to be already present in b.UnitOfMeasureId and b.UOMGroupId is required to be 1. For instance UNITOFMEASURID with value 111 can only be inserted into a if there is a row in b where b.UnitOfMeasureId=111 and b.UOMGROUPIG=1, was that right?
OK, my answer should work that way, you can try the SQL Fiddle example that I added to the answer. If it's not working correctly, post some sample data and expected output (some rows from b and some rows that should and shouldn't be allowed to be inserted into a).
1

You cannot write query within Check constraint. Instead you can call user defined function.

You can use same function in both constraints by passing @UOMGroupId value along with UNITOFMEASURID.

CREATE FUNCTION CheckFnctn(@UNITOFMEASURID int, @UOMGroupId int)
RETURNS int
AS 
BEGIN
    if exists(SELECT UnitOfMeasureId FROM b  WHERE UOMGroupId=@UOMGroupId 
    and UnitOfMeasureId = @UNITOFMEASURID)
    BEGIN
        RETURN 1;
    END

     RETURN 0; --missed this line
END;


ALTER TABLE [a]
ADD CONSTRAINT  UOMGROUPIG CHECK (dbo.CheckFnctn(UNITOFMEASURID, 1)=1);
ALTER TABLE [c]
ADD CONSTRAINT  UOMGROUPIG CHECK (dbo.CheckFnctn(UNITOFMEASURID, 2)=1);

3 Comments

Thanks I am actually checking for the records available there (SELECT UnitOfMeasureId FROM b WHERE UOMGroupId=2 ) this could return as 4 , 78,43,1 so the column UOMGROUPIG should allow these values alone.
It throws an error Msg 455, Level 16, State 2, Procedure CheckFnctn, Line 8 The last statement included within a function must be a return statement.
@ArunKumar: Missed else condition. See my update. Function must return a value, so added RETURN 0 if record is not found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.