你能告诉我如何避免下面提到的例外吗?
INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode)
SELECT DISTINCT
a.City, a.ZipCode
FROM
[Legacy].[dbo].[Ziplist] AS a
WHERE
(a.City IS NOT NULL AND a.ZipCode IS NOT NULL);
例外:
无法将值NULL插入列' IsDeleted',table' Migrated.dbo.Cities&#39 ;;列不允许空值。 INSERT失败。
答案 0 :(得分:7)
正如@ jamieD77所述,您缺少插入语句中的IsDeleted
列。
错误表示该列标记为" NOT NULL"因此,在创建新行时必须插入一个值。
因此,您需要自己从表中删除NULL约束,或在列中插入值。
INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode, IsDeleted)
select DISTINCT a.City,a.ZipCode,0 from [Legacy].[dbo].[Ziplist] as a where (
a.City is not null and a.ZipCode is not null);
对于我认为是bit
字段(但您应该确认!),值0
将为false
而1
将为true
}。如果字段是不同的数据类型,则这些值可能具有不同的含义!!!
答案 1 :(得分:0)
错误消息的内容是[object Error] {description: "Unable to get property 'parentNode' of undefined or null reference", name: "TypeError", number: "-2146823281"}
列已声明IsDeleted
且它没有默认值。在这种情况下,您可能默认插入未删除的记录,因此您可能希望更改列:
NOT NULL
或者,您可以编写查询以包含列:
alter table cities alter column IsDeleted int not null default 0;
这些答案假设INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode, IsDeleted)
select DISTINCT zl.City, zl.ZipCode,
from [Legacy].[dbo].[Ziplist] zl
where a.City is not null and a.ZipCode is not null;
是一个整数,其中" 0"为假。如果值的存储方式不同,则需要对代码进行适当修改。
答案 2 :(得分:0)
只需将列添加到插入列表和固定值即可选择列表。
INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode, IsDeleted)
select DISTINCT a.City,a.ZipCode, 0 IsDeleted --0 means false
from [Legacy].[dbo].[Ziplist] as a
where (a.City is not null and a.ZipCode is not null);