SQL 视图从非空表推断可为空列?
我有一个包含非空“数量”(十进制)和“状态”(整数)列的 Product 表,并使用以下 case 表达式在此表上创建了一个视图:
SELECT P.ProductTypeId,
(CASE WHEN P.StatusId IN (5, 8) THEN 0 ELSE -P.Quantity END) AS Quantity,
...
FROM Product P
ProductTypeId 被正确推断为非空。但是,此视图的 Quantity 列被推断为可为空,即使基础列不可为空。这对我来说没有任何意义。
在这种情况下,我可以使用 ISNULL/COALESCE 提供默认值并强制不可为空,但没有有意义的默认值,根据我的理解,这首先不应该发生。有什么想法吗?
I have a Product table with non-null "quantity" (decimal) and "status" (int) columns, and I created a view on this table with the following case expression:
SELECT P.ProductTypeId,
(CASE WHEN P.StatusId IN (5, 8) THEN 0 ELSE -P.Quantity END) AS Quantity,
...
FROM Product P
ProductTypeId is correctly inferred as non-null. However, the Quantity column of this view is inferred as nullable, even though the underlying columns are not nullable. This doesn't make any sense to me.
I could use ISNULL/COALESCE to provide a default value in this case and force non-nullability, but there is no meaningful default value, and this shouldn't happen in the first place from what I understand. Any ideas what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下说明适用于表中的计算列。我想这同样适用于视图中的计算列。
表达式可能返回 NULL 的示例是
The below explanation is for computed columns in a table. I imagine the same applies to computed columns in a view.
An example where your expression could return
NULL
is