Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

CHECK constraint with Parameterised UDF Expand / Collapse
Author
Message
Posted Thursday, October 23, 2008 10:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 16, 2009 5:41 PM
Points: 1, Visits: 33
-- CREATE TEMP TABLE
CREATE TABLE T
(
Id INT NOT NULL,
IsEnabled bit not null,
NAME VARCHAR(500) NOT NULL
);
GO
-- CREATE FUNCTION
CREATE FUNCTION udfTemp( @Id int)
RETURNS INT
BEGIN
DECLARE @count INT;
SELECT @count = count(*)
FROM t
WHERE id = @id AND
isEnabled = 1
RETURN @count
END
GO
-- ADD CONSTRAINT TO THE TABLE
/*
Now this constraint ensure that there is
ONE AND ONLY ONE record where the is IsEnabled is true for a particular ID.
*/
ALTER TABLE t
WITH CHECK ADD CONSTRAINT ck_t
CHECK (dbo.udftemp(id)=(1))

-- Add some base data.
insert into t values (1,1,'item1')
insert into t values (2,1,'item2')
insert into t values (3,1,'item3')
insert into t values (4,1,'item4')
insert into t values (1,0,'item1 again')

SELECT * FROM T;

/*
Id IsEnabled NAME
----------- --------- ---------------
1 1 item1
2 1 item2
3 1 item3
4 1 item4
1 0 item1 again

(5 row(s) affected)
*/

-- Adding this will give us constraint violation
insert into t values (5,0,'item5') -- because Id 5 does not have a single record where IsEnabled is true
insert into t values (1,1,'item2 should fail') -- becuase Id 1 will have 2 records where IsEnabled is true

-- THE QUESTION?????????
SELECT * FROM T;

-- Why Does this pass?????
-- I am expecting the this should fail Because this update will leave the table in inconsistent state.
-- As shown in the below record set. ID 1 will not have any IsEnabled true
UPDATE T
SET IsEnabled = 0
WHERE Id = 1
AND IsEnabled = 1

SELECT * FROM T;
/*
Id IsEnabled NAME
----------- --------- -------------------
1 0 item1
2 1 item2
3 1 item3
4 1 item4
1 0 item1 again

*/


--------------------------------------------------------------------
-- CLEAN UP
--------------------------------------------------------------------

-- DROP TABLE T;
Post #590959
Posted Friday, October 24, 2008 1:22 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 2:05 AM
Points: 211, Visits: 574
I cannot explain this. Something for the MVP's with knowledge of the actual SQL code, but I do have a solution.

I believe this is a legacy issue with the check constraints from the days of rules and binding (sql7 if memory serves)....The Check constraint is only being evaluated when then Id (the column in the check constraint definition) is being inserted or updated.

The work around is to add the ISEnabled to the Function
CREATE FUNCTION udfTemp( @Id int, @IsEnabled Bit)

This way when you try the update it will fire the Check and fail



Post #591002
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse