Is there any way to create multiple insert statements in a ms-access query?

yes and no.

You can’t do:

insert into foo (c1, c2, c3)
values ("v1a", "v2a", "v3a"),
       ("v1b", "v2b", "v3b"),
       ("v1c", "v2c", "v3c")

but you can do

insert into foo (c1, c2, c3)
    select (v1, v2, v3) from bar

What does that get you if you don’t already have the data in a table? Well, you could craft a Select statement composed of a lot of unions of Selects with hard coded results.

INSERT INTO foo (f1, f2, f3)
    SELECT *
    FROM (select top 1 "b1a" AS f1, "b2a" AS f2, "b3a" AS f3 from onerow
    union all
    select top 1 "b1b" AS f1, "b2b" AS f2, "b3b" AS f3 from onerow
    union all 
    select top 1 "b1c" AS f1, "b2c" AS f2, "b3c" AS f3 from onerow)

Note: I also have to include a some form of a dummy table (e.g., onerow) to fool access into allowing the union (it must have at least one row in it), and you need the “top 1” to ensure you don’t get repeats for a table with more than one row

But then again, it would probably be easier just to do three separate insert statements,
especially if you are already building things up in a loop (unless of course the cost of doing the inserts is greater than the cost of your time to code it).

Leave a Comment