Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CHECK constraint with Parameterised UDF


CHECK constraint with Parameterised UDF

Author
Message
chinkit
chinkit
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 34
-- 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;

AnzioBake
AnzioBake
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 672
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



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search