Solved

Access - Multiple SQl statements

Posted on 2016-08-25
12
59 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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

12 Experts available now in Live!

Get 1:1 Help Now