Microsoft Access is one of the easiest to use database application software with a lot of wizards and graphical utilities to guide the users along. For these reasons, I like to use it for prototyping database designs and processing small sets of data. One of the annoyances with Microsoft Access is that the Query tool does not allow me to run bulk SQL statements at one time as shown in the figure below.
This post shows a simple method to perform a bulk update of records in a Microsoft Access database from SQL statements in a text file using Perl and ODBC. I will also provide a link to download the Perl program
Software Prerequisites
- Download and install ActiveState Perl.
- Download and install the ODBC Driver for Perl DBI.
Define ODBC Data Source
If you know how to use the ODBC Data Source Administrator to define a data source name, then you can skip this section.
- Select Start > Control Panel.
The Control Panel dialog appears. - Click the following: Performance and Maintenance > Administrative Tools > Data Sources (ODBC).
The ODBC Data Source Administrator appears. - Click Add.
The Create New Data Source dialog appears. - Choose Microsoft Access Driver (*.mdb) and click Finish.
The ODBC Microsoft Access Setup dialog appears. - Type in your Data Source Name and click Select and choose your Microsoft Access database file.
- Click OK to close all the dialog boxes.
You can use any tool or text editor to create your text file containing the bulk SQL statements. An example file is shown below.
UPDATE STORES SET BUILDINGNAME='Post Centre' WHERE ID=34;
UPDATE STORES SET BUILDINGNAME='Empire State Building' WHERE ID=3;
UPDATE STORES SET BUILDINGNAME='Twin Towers' WHERE ID=1;
Using the BulkSQL Program
- Download and the BulkSQL.pl program from this link and uncompress the file into a folder of your choice.
- Open a Command Prompt window.
- At the prompt, type in the following commands to run the program.
Note: Replace mydatabase with the actual name of your ODBC data source name to your Access database file. Replace mybulksql.txt with the actual name of your bulk SQL text file.
C:\> perl bulksql.pl mydatabase mybulksql.txt
A sample run is shown in the figure below.
- The program will print out the number of records affected by a single SQL statement.
- The program will print out the SQL statement if it did not affect any records.
Wow thanks for this post! It is really helpful.. best regards, nikolai
ReplyDelete