CHECK option in views:
Views are nothing but an external skeleton for a table. In order to avoid direct access to the table + If we have multiple tables to be linked to obtain a single output. We will use views.
if exists (select * from sys.objects where name='venkat')
begin
drop table venkat
end
if not exists (select * from sys.objects where name='venkat')
begin
create table venkat(id int, name varchar(20),name1 varchar(20))
end
insert into venkat
select 1,'venkat','venkat1'
union all
select 2,'arun','arun1'
union all
select 3,'lakshmi','lakshmi1'
union all
select 4,'karthi','karthi1'
go
insert into venkat select 6,'sajhklg','kjhl55'
--select * from sys.messages ]
drop view venkatview
-- Check option is used to restrict the data by validating at the view level
create view venkatview
as
select * from venkat where id>3 with check option
insert into venkatview select 0,'suba','suba1'
We will get an error message,
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.
Thanks and Regards,
Venkatesan Prabu .J