B.Termeer
B.Termeer

Reputation: 315

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

Upvotes: 3

Views: 3414

Answers (1)

Igor
Igor

Reputation: 62213

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

Upvotes: 5

Related Questions