Managing and debugging SQL queries in MS Access

2018-12-31 02:31发布

MS Access has limited capabilities to manage raw SQL queries: the editor is quite bad, no syntax highlighting, it reformats your raw SQL into a long string and you can't insert comments.

Debugging complex SQL queries is a pain as well: either you have to split it into many smaller queries that become difficult to manage when your schema changes or you end-up with a giant query that is a nightmare to debug and update.

How do you manage your complex SQL queries in MS Access and how do you debug them?

Edit
At the moment, I'm mostly just using Notepad++ for some syntax colouring and SQL Pretty Printer for reformatting sensibly the raw SQL from Access.
Using an external repository is useful but keeping there's always the risk of getting the two versions out of sync and you still have to remove comments before trying the query in Access...

9条回答
时光乱了年华
2楼-- · 2018-12-31 02:39

I guess I don't write complex SQL, because I don't have a problem with the Access SQL editor most of the time. This is because, for the most part, I use the QBE to write the SQL and only dip into the SQL view to do the things the QBE doesn't support (such as non-equi joins, some forms of subqueries, UNION, etc.). This is not to say that I don't have any SQL that is very hard to work with, but that's mostly because it's HIDEOUSLY BADLY WRITTEN, and that's my fault, not Access's fault. I have one horrid, appalling saved QueryDef in an app that's been in production since 1997 that has SQL that's 11,934 characters. And, yes, it's awful to troubleshoot. And nearly any edit I make to it breaks something. But that's because IT'S BAD SQL.

Why anyone would want to write their SQL by hand as a general rule, I can't say. For anything but the most trivial SQL, it seems to me like more trouble than it's worth.

This kind of thing seems to me like another case of people resisting the default Access way of doing things. Almost always, this comes about with users experienced in other programming environments who are too impatient to try things the way Access does them by default. The end result is usually unhappy for everybody.

查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 02:42

I wrote Access SQL Editor-- an Add-In for Microsoft Access-- because I write quite a lot of pass-through queries, and more complex SQL within Access. This add-in has the advantage of being able to store formatted SQL (with comments!) within your Access application itself. When queries are copied to a new Access application, formatting is retained. When the built-in editor clobbers your formatting, the tool will show your original query and notify you of the difference.

It currently does not debug; if there was enough interest, I would pursue this-- but for the time being the feature set is intentionally kept small.

It is not free for the time being, but purchasing a license is very cheap. If you can't afford it, you can contact me. There is a free 14-day trial here.

Once it's installed, you can access it through your Add-Ins menu (In Access 2010 it's Database Tools->Add Ins).

查看更多
笑指拈花
4楼-- · 2018-12-31 02:42

Expanding on this suggestion from Smandoli:

NO:   DoCmd.RunSQL ("SELECT ...")
YES:  strSQL = "SELECT ..."
      DoCmd.RunSQL (strSQL)

If you want to keep the SQL code in an external file, for editing with your favorite text editor (with syntax coloring and all that), you could do something like this pseudo-code:

// On initialization:
global strSQL
f = open("strSQL.sql")
strSQL = read_all(f)
close(f)

// To to the select:
DoCmd.RunSQL(strSQL)

This may be a bit clunky -- maybe a lot clunky -- but it avoids the consistency issues of edit-copy-paste.

Obviously this doesn't directly address debugging SQL, but managing code in a readable way is a part of the problem.

查看更多
荒废的爱情
5楼-- · 2018-12-31 02:46

Similar to recursive, I use an external editor to write my queries. I use Notepad++ with the Light Explorer extension for maintaining several scripts at a time, and Notepad2 for one-off scripts. (I'm kind of partial to Scintilla-based editors.)

Another option is to use the free SQL Server Management Studio Express, which comes with SQL Server Express. (EDIT: Sorry, EdgarVerona, I didn't notice you mentioned this already!) I normally use it to write SQL queries instead of using Access, because I typically use ODBC to link to a SQL Server back end anyway. Beware that the differences in the syntax of T-SQL, used by SQL Server, and Jet SQL, used by Access MDB's, are sometimes substantial.

查看更多
梦该遗忘
6楼-- · 2018-12-31 02:54

If you're doing really complex queries in MS Access, I would consider keeping a repository of those queries somewhere outside of the Access database itself... for instance, in a .sql file that you can then edit in an editor like Intype that will provide syntax highlighting. It'll require you to update queries in both places, but you may end up finding it handy to have an "official" spot for it that is formatted and highlighted correctly.

Or, if at all possible, switch to SQL Server 2005 Express Edition, which is also free and will provide you the features you desire through the SQL Management Studio (also free).

查看更多
几人难应
7楼-- · 2018-12-31 02:56

I have a few tips that are specific to SQL in VBA.

Put your SQL code with a string variable. I used to do this:

DoCmd.RunSQL "SELECT ..."

That is hard to manage. Do this instead:

strSQL = "SELECT ..."
DoCmd.RunSQL strSQL

Often you can't fix a query unless you see just what's being run. To do that, dump your SQL to the Immediate Window just before execution:

strSQL = "SELECT ..."
Debug.Print strSQL
Stop
DoCmd.RunSQL strSQL

Paste the result into Access' standard query builder (you must use SQL view). Now you can test the final version, including code-handled variables.

When you are preparing a long query as a string, break up your code:

strSQL = "SELECT wazzle FROM bamsploot" _
      & vbCrLf & "WHERE plumsnooker = 0"

I first learned to use vbCrLf when I wanted to prettify long messages to the user. Later I found it makes SQL more readable while coding, and it improves the output from Debug.Print. (Tiny other benefit: no space needed at end of each line. The new line syntax builds that in.)

(NOTE: You might think this will let you add add comments to the right of the SQL lines. Prepare for disappointment.)

As said elsewhere here, trips to a text editor are a time-saver. Some text editors provide better syntax highlighting than the official VBA editor. (Heck, StackOverflow does better.) It's also efficient for deleting Access cruft like superfluous table references and piles of parentheses in the WHERE clause.

Work flow for serious trouble shooting:

VBA Debug.Print >       (capture query during code operation)
  query builder   >     (testing lab to find issues)
     Notepad++      >   (text editor for clean-up and review)
  query builder   >     (checking, troubleshooting) 
VBA

Of course, trouble shooting is usually a matter of reducing the complexity of a query until you're able to isolate the problem (or at least make it disappear!). Then you can build it back up to the masterpiece you wanted. Because it can take several cycles to solve a sticky problem, you are likely to use this work flow repeatedly.

查看更多
登录 后发表回答