Question

I am using MS Access 2003. I want to run a lot of insert SQL statements in what is called 'Query' in MS Access. Is there any easy(or indeed any way) to do it?

Was it helpful?

Solution

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).

OTHER TIPS

Personally, I'd create a VBA subroutine to do it, and connect to the database using some form of sql connection.

Off the top of my head, the code to do it should look something like:

Sub InsertLots ()
    Dim SqlConn as Connection
    SqlConn.Connect("your connection string")
    SqlConn.Execute("INSERT <tablename> (column1, column2) VALUES (1, 2)")
    SqlConn.Execute("INSERT <tablename> (column1, column2) VALUES (2, 3)")
    SqlConn.Close()
End Sub

I think it's inadvisable to propose a particular data interface, as Jonathan does, when you haven't clarified the context in which the code is going to run.

If the data store is a Jet database, it makes little sense to use any form of ADO unless you're running your code from a scripting platform where it's the preferred choice. If you're in Access, this is definitely not the case, and DAO is the preferred interface.

No - a query in Access is a single SQL statement. There is no way of creating a batch of several statements within one query object. You could create multiple query objects and run them from a macro/module.

@Rik Garner: Not sure what you mean by 'batch' but the

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)

construct, although being a single SQL statement, will actually insert each row one at a time (rather than all at once) but in the same transaction: you can test this by adding a relevant constraint e.g.

ALTER TABLE foo ADD
   CONSTRAINT max_two_foo_rows
      CHECK (2 >= (SELECT COUNT(*) FROM foo AS T2));

Assuming the table is empty, the above INSERT INTO..SELECT.. should work: the fact it doesn't is because the constraint was checked after the first row was inserted rather than the after all three were inserted (a violation of ANSI SQL-92 but that's MS Access for you ); the fact the table remains empty shows that the internal transaction was rolled back.

@David W. Fenton: you may have a strong personal preference for DAO but please do not be too hard on someone for choosing an alternative data access technology (in this case ADO), especially for a vanilla INSERT and when they qualify their comments with, " Off the top of my head, the code to do it should look something like…" After all, you can't use DAO to create a CHECK constraint :)

MS Access does not allow multiple insert from same sql window. If you want to insert, say 10 rows in table, say movie (mid, mname, mdirector,....), you would need to open the sql windows,

  1. type the 1st stmt, execute 1st stmt, delete 1st stmt
  2. type the 2nd stmt, execute 2nd stmt, delete 2nd stmt
  3. type the 3rd stmt, execute 3rd stmt, delete 3rd stmt ......

Very boring. Instead you could import the lines from excel by doing:

  1. Right-click on the table name that you have already created
  2. Import from Excel (Import dialog box is opened)
  3. Browse to the excel file containing the records to be imported in the table
  4. Click on "Append a copy of the records to the table:"
  5. Select the required table (in this example movie)
  6. Click on "OK"
  7. Select the worksheet that contains the data in the spreadsheet
  8. Click on Finish

The whole dataset in the excel has been loaded in the table "MOVIE"

MS Access can also Append data into a table from a simple text file. CSV the values (I simply used the Replace All box to delete all but the commas) and under External Data select the Text File.

From this:
INSERT INTO CLASS VALUES('10012','ACCT-211','1','MWF 8:00-8:50 a.m.','BUS311','105');
INSERT INTO CLASS VALUES('10013','ACCT-211','2','MWF 9:00-9:50 a.m.','BUS200','105');
INSERT INTO CLASS VALUES('10014','ACCT-211','3','TTh 2:30-3:45 p.m.','BUS252','342');
To this:
10012,ACCT-211,1,MWF 8:00-8:50 a.m.,BUS311,105
10013,ACCT-211,2,MWF 9:00-9:50 a.m.,BUS200,105
10014,ACCT-211,3,TTh 2:30-3:45 p.m.,BUS252,342
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top