我将数据从JSON文件加载到"main.jsontable“表中,触发的任务是将所有不同国家的数据从"main.jsontable”插入到"main.country“表中。我的问题是触发器需要处理插入多个行,我的当前代码是:
create or alter trigger main.afterParsing
on main.jsontable
after insert
as
begin
declare @country nvarchar(50);
insert into main.country(countryName)
values(@country)
end;但是我得到了这个错误(很明显,因为触发器一次只能处理插入1行):
无法将值NULL插入“countryName”列、表“转让人2.main.Country”中;列不允许空值。插入失败。
有人知道如何使用触发器插入多行吗?
谢谢
发布于 2018-05-09 16:28:53
您需要使用Inserted伪表,并且需要理解它可以包含多行(如果您的INSERT语句一次插入超过1行),并且需要使用正确的基于集的方法来相应地处理该表。
试着做这样的事情:
create or alter trigger main.afterParsing
on main.jsontable
after insert
as
begin
insert into main.country(countryName)
select countryName
from Inserted
-- if the data being inserted could have NULL in the
-- countryName - filter those entries out
where countryName is not null
end;https://stackoverflow.com/questions/50257993
复制相似问题