tags:

views:

3836

answers:

6

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?

A: 

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
+1  A: 

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
Jonathan
+10  A: 

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

CodeSlave
Very interesting and descriptive answer. It will definitely help me in what I was doing
Varun Mahajan
+1  A: 

Just want to second the opinion of CodeSlave. I've done this before using the technique he describes, and it works. For the record, the same thing works in SQL Server and Oracle as well.

JosephStyons
+1  A: 

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.

David-W-Fenton
A: 

@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 :)

onedaywhen
MS left certain features out of DAO on purpose back when MS was promoting ADO over DAO. My hope is that with Access now having its own private version of Jet, DAO will be updated to support everything that Jet itself supports.
David-W-Fenton
I too hope your are correct. But until they do, ADO is still required to plug the holes in DAO: CHECK constraints, WITH COMPRESSION, row level locking, etc. If the poster hasn't "clarified the context" then surely answers can be in ADO or DAO, a mere lifestyle choice, no? :)
onedaywhen
How does DAO function when you upgrade to SQL Server?
Jeff O