Solved

Access - Multiple SQl statements

Posted on 2016-08-25
12
91 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

738 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