开发者

How to create a column that can never be updated?

开发者 https://www.devze.com 2022-12-31 12:52 出处:网络
In SQL Server 2008, is it possible to create a column that can have a value inserted into it, but can never be updated? In other words, the column can have an initial value inserted into it, but once

In SQL Server 2008, is it possible to create a column that can have a value inserted into it, but can never be updated? In other words, the column can have an initial value inserted into it, but once it contains a non-nu开发者_如何学Goll value, it can never be changed. If possible, I would prefer to do it without using a trigger.

Thanks - Randy


You can't define a column as Read Only but can you not achieve your goal by setting permission against the column so it can be inserted but not updated by all the relevant users/roles/groups in your database?

Edit:

I misread a bit of your spec, if you need to allow someone to "insert" a null and it only goes read only when a null value is entered then you probably would need a trigger - either to block the updates or to set the deny permissions for the column after a non null value is entered.


You'll have to use a trigger.

You can GRANT INSERT and DENY UPDATE on the column itself, but that would stop you from being able to UPDATE from NULL to something after the initial INSERT.

0

精彩评论

暂无评论...
验证码 换一张
取 消