Solved

Access - Multiple SQl statements

Posted on 2016-08-25
12
73 Views
Last Modified: 2016-09-12
Is it possible to execute  Multiple SQl statements from .NET code using the OLE DB objects?  if I try using two INSERT statement, each ending with a semicolon, I get an error - "Characters found after end of SQL statement. "
0
Comment
Question by:HLRosenberger
  • 6
  • 3
  • 3
12 Comments
 
LVL 2

Expert Comment

by:Antonio Salva Ripoll
ID: 41770643
Hi HLRosenberger.

In Access, SQL Server, Oracle.... the SQL action querys must be executed one after other. So, you must call the first "Insert ..." and later the second "Insert...".

Best regards.

Antonio.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41770653
can we see your query? You normally don't have to put the ; as the syntax is quite rigid.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 41770675
It's just two inserts.  I do this using SQL server all the time.

"INSERT INTO tax_status (id, code, taxable) VALUES(1234, 'DDDD', 0); " & _
                                                                "INSERT INTO tax_status (id, code, taxable) VALUES(1235, 'ffff', 0);")
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41770697
have you tried removing the ; ?
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 41770716
if I do that, I get "Missing semicolon at end of SQL statement".
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 41770723
bottom line - I would like to do a "batch" update to an access database.   Is that possible?  Like execute multiple inserts/updates in one action?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 41770764
Does Access support anything like Stored Procs?   Can I write a macro or VBA script that I can execute from .NET code?
0
 
LVL 2

Expert Comment

by:Antonio Salva Ripoll
ID: 41770786
Hi @HLRosenberger.

To the question if Access supports stored procedures, the answer is "no in the same way as SQL Server".

Although you can use VBA to store and execute SQL statements, you must put them one after another.

This is the VBA code to execute your Inserts:
Public Sub RunMyInserts()
    CurrentDb.Execute "INSERT INTO tax_status (id, code, taxable) VALUES(1234, 'DDDD', 0);"
    CurrentDb.Execute "INSERT INTO tax_status (id, code, taxable) VALUES(1235, 'ffff', 0);"
End Sub

Open in new window


Also, if you can pass parameters to the procedure, you can do a "batch"
Sub TestParams()
    
    RunMyInserts "1234, 'DDDD', 0"
    RunMyInserts "1235, 'ffff', 0"

End Sub

Public Sub RunMyInserts(MyParams As String)
    CurrentDb.Execute "INSERT INTO tax_status (id, code, taxable) VALUES(" & MyParams & ");"
End Sub

Open in new window


Best regards.

Antonio (Barcelona, Spain)
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41770806
from what I have read elsewhere, it doesn't seem to be possible but maybe there are some workarounds for example:
INSERT INTO tax_status (id, code, taxable) 
select * from (
   select 1234, 'DDDD', 0
   union all 
   select 1235, 'ffff', 0
);

Open in new window

0
 
LVL 2

Expert Comment

by:Antonio Salva Ripoll
ID: 41771003
@Éric, great solution.

As I always said, "the best solutions are always the simplest ones".

My sincere congratulations.

Antonio.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 41771761
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 41791491
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now