Set a column on a populated table to not allow nulls where lots of rows already are set to null
I've got a table with an active column of type bit that is mostly set to null. The active column was added as an after thought and mistakenly set to allow nulls. So SQL Server won't let me turn off the null flag because most of the fields are already set to null.
I tried setting a default value of 1 to the column and then removing the null flag but it wasn't having any of that. I think the default value only applies to inserts and not an alter.
I then tried to set all the null values to 1 explicitly with the following query:
Update cnc.aidPage set active = 1 where active = null
Unfortunately this doesn't affect any rows. I tried derivatives of this and found that it doesn't change anything only when the filter is trying for null. So the following work:
Update cnc.aidPage set active = 1 where active = 0
Update cnc.aidPage set active = null where active = 1
I would appreciate it if anyone could tell me why the filter doesn't work if I check for null or if there is an easier way to accomplish turning allow nulls off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NULL isn't a value -- it's a placeholder to indicate the lack of any value, so you can't use math operators to check for it. You need to use:
Your problem is that "null" in most SQL implementations, unlike most imperative languages, doesn't "equal" anything. NULL doesn't even equal NULL explicitly. You have to use the special "is" operator to do null checks:
This should work for you.
You can't use '
=
' to check for NULL. Instead use'IS NULL'
:NULL is used as a placeholder for unknown or inapplicable values.
Try: