insert into DWAdventureWorksLT2012Lab01.[dbo].[DimProducts]
(
[ProductID],
[ProductName],
[ProductColor],
[ProductListPrice],
[ProductSize],
[ProductWeight],
[ProductCategoryID],
[ProductCategoryName]
)
select P1.ProductID,
P1.Name,
P1.Color,
P1.ListPrice,
P1.Size,
P1.Weight,
P2.ProductCategoryID,
P2.Name from [AdventureWorksLT2012].[SalesLT].[Product] P1
JOIN [AdventureWorksLT2012].[SalesLT].[ProductCategory] P2
on P1.ProductCategoryID = P2.ProductCategoryID
where ProductID IS NOT NULL我得到了这个:
"Msg 515、级别16、状态2、行206不能将值NULL插入'ProductColor‘列,表'DWAdventureWorksLT2012Lab01.dbo.DimProducts';列不允许空值。插入失败。语句已终止。“
能解决这个问题吗?
发布于 2018-03-22 13:53:17
通过使用聚结()或ISNULL()函数,可以避免选择句子中的空值。
按顺序计算参数,并返回最初不计算为NULL的第一个表达式的当前值。例如,选择COALESCE( null,NULL,' third _ value ','fourth_value');返回第三个值,因为第三个值是非空的第一个值。
用指定的替换值替换NULL。
INSERT INTO DWAdventureWorksLT2012Lab01.[dbo].[DimProducts]
(
[ProductID],
[ProductName],
[ProductColor],
[ProductListPrice],
[ProductSize],
[ProductWeight],
[ProductCategoryID],
[ProductCategoryName]
)
SELECT P1.ProductID,
P1.Name,
COALESCE(P1.Color, 0), -- or '' if it is a char column
P1.ListPrice,
ISNULL(P1.Size, 0),
P1.Weight,
P2.ProductCategoryID,
P2.Name from [AdventureWorksLT2012].[SalesLT].[Product] P1
JOIN [AdventureWorksLT2012].[SalesLT].[ProductCategory] P2
ON P1.ProductCategoryID = P2.ProductCategoryID
WHERE ProductID IS NOT NULL;发布于 2018-03-22 14:53:49
列ProductColor是非NULLABLE。你可以在DDL中看到这一点。
这意味着你需要确保每个产品都有一个颜色--或者如果你想改变设计,让列为空。我不喜欢使用像0或空白这样的place holder值。这是NULL的最佳情况。
https://dba.stackexchange.com/questions/202009
复制相似问题