Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access - Multiple SQl statements

Posted on 2016-08-25
12
Medium Priority
?
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 70

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
 
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 70

Accepted Solution

by:
Éric Moreau earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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