Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 WITH CHECK OPTION is not checking for deletes

Author  Topic 

ekareem
Starting Member

9 Posts

Posted - 2006-05-13 : 00:02:15
I was testing how the WITH CHECK OPTION works. I though that it prevents row from dissappearing from the view implementing this option but I found that I was able to DELETE all rows through the view - Why is this???

Please note that I only have the base version of sql server 2000 - No patches applied

Thanks for your help

------------------------------------------- Test

Create table T100 (A int)
GO
Create view VT100 AS (SELECT * FROM T100 WHERE A < 2) with check option
GO
INSERT INTO T100 VALUES (1) -- Part of the view's result
INSERT INTO T100 VALUES (2)
GO
SELECT * FROM VT100 -- SHOWS 1
GO
INSERT INTO VT100 VALUES (-2) -- Works
GO
UPDATE VT100 SET A=5 -- Update fails because of WITH CHECK OPTION - GOOD
GO
INSERT INTO VT100 VALUES (999) -- Inser fails because of WITH CHECK OPTION - GOOD
GO
DELETE FROM VT100 -- DELETES ALL ROWS!!!! ******************* ???


ekareem

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-05-13 : 01:16:06
Worked OK for me:

drop table T100
go
drop view VT100
go
Create table T100 (A int)
GO
Create view VT100 AS (SELECT * FROM T100 WHERE A < 2) with check option
GO
INSERT INTO T100 VALUES (1) -- Part of the view's result
INSERT INTO T100 VALUES (2)
GO
SELECT * FROM VT100 -- SHOWS 1
GO
INSERT INTO VT100 VALUES (-2) -- Works
GO
UPDATE VT100 SET A=5 -- Update fails because of WITH CHECK OPTION - GOOD
GO
INSERT INTO VT100 VALUES (999) -- Inser fails because of WITH CHECK OPTION - GOOD
GO
DELETE FROM VT100 -- DELETES ALL ROWS!!!! ******************* ???
go
print 'Select from T100'
SELECT * FROM T100 -- Still has one row

Results:

(1 row(s) affected)


(1 row(s) affected)

A
-----------
1

(1 row(s) affected)


(1 row(s) affected)

Server: Msg 550, Level 16, State 1, Line 1
The attempted insert or update failed because the target view either specifies WITH CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from the operation did not qualify under the CHECK OPTION constraint.
The statement has been terminated.
Server: Msg 550, Level 16, State 1, Line 1
The attempted insert or update failed because the target view either specifies WITH CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from the operation did not qualify under the CHECK OPTION constraint.
The statement has been terminated.

(2 row(s) affected)

Select from T100
A
-----------
2

(1 row(s) affected)



CODO ERGO SUM
Go to Top of Page

ekareem
Starting Member

9 Posts

Posted - 2006-05-14 : 00:13:32
Hi,
Thanks for your help. The code works however, the strange thing is that DELETE from the VIEW defined with WITH CHECK OPTION actully deletes rows - The option is supposed to prevent rows from disappearing from the view but it does not do that in case of Delete!!!

Any idea why?
Thanks.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-05-14 : 02:22:15
From BOL

quote:

WITH CHECK OPTION clause forces all data modification statements executed against the view to adhere to the criteria set within the SELECT statement defining the view. If you use this clause, rows cannot be modified in a way that causes them to disappear from the view. Any modification that would cause this to happen is canceled and an error is displayed



It Deleted the rows which are satisfying the crieatira mentioned in the where cluase

If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2006-05-15 : 04:27:35
I think you'd probably need to use some kind of INSTEAD OF trigger to achieve what you want to achieve here.

-------
Moo. :)
Go to Top of Page

Kaliaa
Starting Member

1 Post

Posted - 2012-08-25 : 03:30:19
@@ekareem
--> WITH CHECK OPTION Only makes sure the data remains visible through the view after only modification is committed. (not Deletion)
Go to Top of Page
   

- Advertisement -