首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Server使用触发器插入多行

问Server使用触发器插入多行
EN

Stack Overflow用户
提问于 2018-05-09 16:23:57
回答 1查看 2.7K关注 0票数 2

我将数据从JSON文件加载到"main.jsontable“表中,触发的任务是将所有不同国家的数据从"main.jsontable”插入到"main.country“表中。我的问题是触发器需要处理插入多个行,我的当前代码是:

代码语言:javascript
复制
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”中;列不允许空值。插入失败。

有人知道如何使用触发器插入多行吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-09 16:28:53

您需要使用Inserted伪表,并且需要理解它可以包含多行(如果您的INSERT语句一次插入超过1行),并且需要使用正确的基于集的方法来相应地处理该表。

试着做这样的事情:

代码语言:javascript
复制
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;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50257993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档