Define a computed column in view as not null
I have a view with computed columns now I need to enforce the computed columns to be "not null
" for lightswitch.
I tried to cast()
but I still cant get the computed columns to be "not null
"
Is it possible?
This is my SQL:
SELECT dbo.Nop_Manufacturer.Name,
CAST(SUM(dbo.Nop_OrderProductVariant.PriceExclTax) AS INT) AS
SALES,
CAST(MONTH(dbo.Nop_Order.PaidDate) AS INT) AS
paid_month,
CAST(YEAR(dbo.Nop_Order.PaidDate) AS INT) AS
paid_year,
CAST(COUNT(dbo.Nop_OrderProductVariant.OrderProductVariantID) AS INT)AS
num_prod_sold
FROM dbo.Nop_ProductVariant
INNER JOIN dbo.Nop_OrderProductVariant
ON dbo.Nop_ProductVa开发者_运维知识库riant.ProductVariantId =
dbo.Nop_OrderProductVariant.ProductVariantID
INNER JOIN dbo.Nop_Product
ON dbo.Nop_ProductVariant.ProductID = dbo.Nop_Product.ProductId
INNER JOIN dbo.Nop_Product_Manufacturer_Mapping
INNER JOIN dbo.Nop_Manufacturer
ON dbo.Nop_Product_Manufacturer_Mapping.ManufacturerID =
dbo.Nop_Manufacturer.ManufacturerID
ON dbo.Nop_Product.ProductId =
dbo.Nop_Product_Manufacturer_Mapping.ProductID
INNER JOIN dbo.Nop_Order
ON dbo.Nop_OrderProductVariant.OrderID = dbo.Nop_Order.OrderID
WHERE ( NOT ( dbo.Nop_Order.PaidDate IS NULL ) )
GROUP BY dbo.Nop_Manufacturer.Name,
MONTH(dbo.Nop_Order.PaidDate),
YEAR(dbo.Nop_Order.PaidDate)
CAST
-ing as INT
won't do anything to affect the perceived nullability of computed columns. You need to wrap them in ISNULL
instead.
e.g. ISNULL(YEAR(dbo.Nop_Order.PaidDate),0)
This is documented in the last paragraph here
The Database Engine automatically determines the nullability of computed columns based on the expressions used. The result of most expressions is considered nullable even if only nonnullable columns are present, because possible underflows or overflows will produce null results as well. Use the
COLUMNPROPERTY
function with theAllowsNull
property to investigate the nullability of any computed column in a table. An expression that is nullable can be turned into a nonnullable one by specifyingISNULL(check_expression, constant)
, where the constant is a nonnull value substituted for any null result.
精彩评论