简体   繁体   中英

MS Access insert multiple rows

For a project I want to use a query to insert multiple rows into a table. I found several threads on how to do this, for example this one , that were really helpful but I still can't figure it out how to insert multiple rows.

The SQL code that I currently have doesn't cause any syntax error, but it just doesn't insert any rows.

My table that I want to insert into looks like:

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

The query that I'm currently using (I made it shorter):

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
        )

Solution:

I added an empty row into table onerow instead of filling it with some data.

This is necessary

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)

Comments:

  • Use single quotes for literal string values
  • Do not use quotes for literal numbers
  • the table onerow must contain at least 1 record

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM