Creating readonly views in Sql Server

后端 未结 3 1006
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 18:17

According to MSDN, views composed of simple selects automatically allow you to use insert/update/delete statements on the table. Is there a way to prevent this - to tell Sql

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 18:40

    You could specify an UNION operator in order to make SQL Server fail during the INSERT/UPDATE/DELETE operation, like this:

    create view SampleView
    as
      select ID, value from table
      union all
      select 0, '0' where 1=0
    

    The last query doesn't return any rows at all, but must have the same amount of fields with the same data types as the first query, in order to use the UNION safely. See this link for more info: Different ways to make a table read only in a SQL Server database

提交回复
热议问题