I have a text file with a few SQL statements in it that I want to run on an Access database. I thought that should be possible with Access' Query Editor. So, I go into this editor and paste the statements:
insert into aFewYears (yr) values ('2000') insert into aFewYears (yr) values ('2001') insert into aFewYears (yr) values ('2002') insert into aFewYears (yr) values ('2003')
Trying to run them (by hitting the red exclamation mark) I receive aMissing semicolon (;) at end of SQL statement.
This could be taken as an indication that the editor would allow to execute multiple statements. So, I change the statements and append such a semicolon at the end:
insert into aFewYears (yr) values ('2000'); insert into aFewYears (yr) values ('2001'); insert into aFewYears (yr) values ('2002'); insert into aFewYears (yr) values ('2003');
Then I get aCharacters found after end of SQL statement.
which probably could be taken as an indication that it is not possible to execute multiple statements.
Ok, so the question: is it possible to execute multiple statements in the query editor, or is it possible to somehow batch-execute sql statements in a file in/on/against Access.
Thanks / Rene
edit The insert statements were used as an example and I realize that they are less than perfect, because they all go to the same table and such a thing can obviously somehow be solved by using one statement that has a union or something. In my actual case that I am trying to solve, the file contains not only insert statements but also create table statements and insert statements with different underlying tables. So I hoped (and still hope) that there is something like my beloved SQL*Plus for Oracle that can execute a file with all kinds of SQL Statements.
If you want to run the queries in one go from Access then create a Macro and on the first line under "Action" select "OpenQuery" from the drop down list, click in the field "Query Name" and select your first query from the drop down list. Then on the 2nd line of the macro do the same for the 2nd query and so on.
JDBC commands can be used to perform SQL operations from the Java application. Demonstrating execution of multiple SQL commands on a database simultaneously using the addBatch() and executeBatch() commands of JDBC.
Simply put three queries one after the other in a . sql file, with semi-colons after each statement, then execute it as a script (either on a SQL*Plus prompt using @scriptname. sql or in TOAD/SQL Developer [or equivalent] using its script execution function).
The addBatch() method of Statement, PreparedStatement, and CallableStatement is used to add individual statements to the batch. The executeBatch() is used to start the execution of all the statements grouped together.
You can easily write a bit code that will read in a file. You can either assume one sql statement per line, or assume the ;
So, assuming you have a text file such as:
insert into tblTest (t1) values ('2000'); update tbltest set t1 = '2222' where id = 5; insert into tblTest (t1,t2,t3) values ('2001','2002','2003');
Note the in the above text file we free to have sql statements on more then one line.
the code you can use to read + run the above script is:
Sub SqlScripts() Dim vSql As Variant Dim vSqls As Variant Dim strSql As String Dim intF As Integer intF = FreeFile() Open "c:\sql.txt" For Input As #intF strSql = input(LOF(intF), #intF) Close intF vSql = Split(strSql, ";") On Error Resume Next For Each vSqls In vSql CurrentDb.Execute vSqls Next End Sub
You could expand on placing some error msg if the one statement don't work, such as
if err.number <> 0 then debug.print "sql err" & err.Descripiton & "-->" vSqls end dif
Regardless, the above split() and string read does alow your sql to be on more then one line...
Unfortunately, AFAIK you cannot run multiple SQL statements under one named query in Access in the traditional sense.
You can make several queries, then string them together with VBA (DoCmd.OpenQuery
if memory serves).
You can also string a bunch of things together with UNION if you wish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With