MS Access插入多行

时间:2017-06-26 14:20:22

标签: sql ms-access-2010 insert-into

对于项目,我想使用查询将多行插入表中。我发现了几个关于如何执行此操作的线程,例如this one,这些线程非常有用,但我仍然无法弄清楚如何插入多行。

我目前拥有的SQL代码不会导致语法错误,但它不会插入任何行。

我要插入的表格如下:

create table SYT_ABRDAT
(
    id integer primary key not null,
    beginper integer,
    eindper integer,
    periode text,
    groep bit
)

我正在使用的查询(我缩短了时间):

insert into syt_abrdat (id, begindat, einddat, periode, groep) 
    select * 
    from
        (select top 1 
             "1" as id, "9999" as begindat, "9999" as einddat,
             "---" as periode, "1" as groep 
         from 
             onerow 
         union all
         select top 1 
             "2" as id, "9999" as begindat, "9999" as einddat,
             "XXX" as periode, "1" as groep 
         from 
             onerow
        )

解决方案:

我在表onerow中添加了一个空行,而不是用一些数据填充它。

这是必要的

1 个答案:

答案 0 :(得分:3)

INSERT INTO syt_abrdat (id,begindat,einddat,periode,groep) 
SELECT * FROM 
   (SELECT TOP 1 1 AS id, 9999 AS begindat, 9999 as einddat, '---' as periode, 'WAAR' as groep FROM onerow UNION ALL
    SELECT TOP 1 2 AS id, 9999 AS begindat, 9999 as einddat, 'XXX' as periode, 'WAAR' as groep FROM onerow)

评论:

  • 对文字字符串值使用单引号
  • 不要使用文字数字的引号
  • onerow必须包含至少1条记录