Index: trunk/ippTools/src/regtool.c
===================================================================
--- trunk/ippTools/src/regtool.c	(revision 17611)
+++ trunk/ippTools/src/regtool.c	(revision 17618)
@@ -430,19 +430,20 @@
 
     // treat limit == 0 as "no limit"
+    psString limitString = NULL;
     if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    // close subquery
-    psStringAppend(&query, "\n    ) as Foo\n");
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
+        limitString = psDBGenerateLimitSQL(limit);
+        // skip past the "hook" comment
+        psStringPrepend(&limitString, "\n");
+    }
+
+    // 1st arg: where hook, 2nd arg: limit hook
+    if (!p_psDBRunQuery(config->dbh, query, "", limitString ? limitString : "")) {
         // XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
         psError(PXTOOLS_ERR_PROG, false, "database error");
+        psFree(limitString);
         psFree(query);
         return false;
     }
+    psFree(limitString);
     psFree(query);
 
@@ -558,7 +559,5 @@
     }
 
-    // close subquery (left open for limit supplied to pendingexpMode (regtool.c:104)
-    psStringAppend(&query, "\n    ) as Foo\n");
-
+    psString whereClause = NULL;
     {
         // build a query to search by exp_id
@@ -571,17 +570,20 @@
         }
 
-        psString whereClause = psDBGenerateWhereSQL(where, NULL);
+        whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psFree(where);
         if (whereClause) {
-        psStringAppend(&query, " %s", whereClause);
+            // skip past comment "hook"
+            psStringPrepend(&whereClause, "\n AND ");
+        }
+    }
+
+    // 1st arg: where hook, 2nd arg: limit hook
+    if (!p_psDBRunQuery(config->dbh, query, whereClause ? whereClause : "", "")) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(whereClause);
-        }
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
         return false;
     }
+    psFree(whereClause);
     psFree(query);
 
