Christian Findlay
Christian Findlay

Reputation: 7712

Validate Microsoft Access SQL

I have a long set of SQL scripts. They are all update statements. It's for an access database. I want to validate the script before I run it. Firstly, I'd like to make sure that the query can be parsed. I.e. that the SQL is at least syntactically correct. Secondly, I'd like to make sure that the query is valid in terms of database structure - i.e. there are no missing columns or the columns are of the wrong type etc. But, I don't want the query to be actually executed. The aim of this is to do a quick validation before the process kicks off because the process takes several hours and one syntactical error can waste a day of someone's time.

I will probably write the tool in C# with .net but if there's a pre-built tool that would be even better. I will probably use the Access API. In SQL Server this is very straight forward. You can just validate the query in SQL Server management studio before running it. It will give you a good indication of whether the SQL will complete or not.

How would I go about doing this?

Edit: an answer below solves the issue of checking syntax. However, I'd still like to be able to validate the semantic content of the query is OK. However, I think this might be impossible in Access without actually running the query. Please tell me I'm wrong.

Upvotes: 1

Views: 2555

Answers (2)

Hambone
Hambone

Reputation: 16417

I'm not 100% sure if Access works the same way as a traditional database, but with a mainstream RDMBS, there are actually three distinct steps that happen when you run a query:

  • Prepare
  • Execute
  • Fetch

Most are oblivious to the distinction because they just hit "run" and see results come back.

It's the "Execute" that actually compiles the statement before going off and pulling data.

When you use ADO, you can actually see the three events as three separate calls to the database. What this means is you can trap the execute step to see if it fails, and if it succeeds, there is nothing requiring you to actually get the results.

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = String.Format("{0}{1}",
    @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=", @"c:\Access\MyDb.accdb");
conn.Open();

bool valid;
using (OleDbCommand cmd = new OleDbCommand("select [Bad Field] from [Table]", conn))
{
    try
    {
        OleDbDataReader reader = cmd.ExecuteReader();
        valid = true;
        reader.Close();   // Did not ever call reader.Read()
    }
    catch (Exception ex)
    {
        valid = false;
    }
}

And now valid indicates whether or not the statement compiled.

If you want to get really fancy, you can parse the exception results to find out why the command failed.

Upvotes: 2

John Mo
John Mo

Reputation: 1326

Access supports transactions on its Connection object. Try to execute your SQL statement inside a transaction and always call Rollback. Wrap the whole attempt in a Try/Catch block to assess whether the statement executed successfully or not.

Upvotes: 0

Related Questions