Index: /trunk/ippTools/share/camtool_find_pendingexp.sql
===================================================================
--- /trunk/ippTools/share/camtool_find_pendingexp.sql	(revision 14146)
+++ /trunk/ippTools/share/camtool_find_pendingexp.sql	(revision 14147)
@@ -2,26 +2,28 @@
 -- does a little more work then is necessary for -addprocessed but it seems
 -- "cleaner" to use the same query for both cases 
-SELECT
-    camRun.*,
-    rawExp.exp_tag,
-    rawExp.exp_id,
-    rawExp.exp_name,
-    rawExp.camera,
-    rawExp.telescope,
-    rawExp.filelevel
-FROM camRun
-JOIN chipRun
-    USING(chip_id)
-JOIN chipProcessedImfile
-    USING(chip_id)
-JOIN rawExp
-    ON chipProcessedImfile.exp_id = rawExp.exp_id
-LEFT JOIN camProcessedExp
-    ON camRun.cam_id = camProcessedExp.cam_id
-LEFT JOIN camMask
-    ON camRun.label = camMask.label
-WHERE
-    chipRun.state = 'stop'
-    AND camRun.state = 'run'
-    AND camMask.label IS NULL
-    AND camProcessedExp.cam_id IS NULL
+SELECT * FROM
+    (SELECT
+        camRun.*,
+        rawExp.exp_tag,
+        rawExp.exp_id,
+        rawExp.exp_name,
+        rawExp.camera,
+        rawExp.telescope,
+        rawExp.filelevel
+    FROM camRun
+    JOIN chipRun
+        USING(chip_id)
+    JOIN chipProcessedImfile
+        USING(chip_id)
+    JOIN rawExp
+        ON chipProcessedImfile.exp_id = rawExp.exp_id
+    LEFT JOIN camProcessedExp
+        ON camRun.cam_id = camProcessedExp.cam_id
+    LEFT JOIN camMask
+        ON camRun.label = camMask.label
+    WHERE
+        chipRun.state = 'stop'
+        AND camRun.state = 'run'
+        AND camMask.label IS NULL
+        AND camProcessedExp.cam_id IS NULL
+    ) as Foo
Index: /trunk/ippTools/src/camtool.c
===================================================================
--- /trunk/ippTools/src/camtool.c	(revision 14146)
+++ /trunk/ippTools/src/camtool.c	(revision 14147)
@@ -590,5 +590,26 @@
     }
 
-    if (!p_psDBRunQuery(config->dbh, query, cam_id)) {
+    {
+        // build a query to search by cam_id
+        psMetadata *where = psMetadataAlloc();
+        if (cam_id) {
+            if (!psMetadataAddS64(where, PS_LIST_TAIL, "cam_id", 0, "==", (psS64)atoll(cam_id))) {
+                psError(PS_ERR_UNKNOWN, false, "failed to add item cam_id");
+                psFree(where);
+                psFree(query);
+                return false;
+            }
+        }
+
+        psString whereClaus = psDBGenerateWhereSQL(where, NULL);
+        psFree(where);
+        if (whereClaus) {
+            psStringAppend(&query, " %s", whereClaus);
+            psFree(whereClaus);
+        }
+
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
