Index: trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 7252)
+++ trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 7254)
@@ -177,11 +177,15 @@
 Bool_t CheckSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
 {
-    Int_t rc=0; //rc=0 means sequence is still the same -> insert not neccessary
+    Int_t rc=0;
+    //rc=0 means sequence not found -> insert
     //rc=1 means deleting sequence(s) worked -> insert
+    //rc=2 means sequence is still the same -> insert not neccessary
     //if deleting sequence doesn't work -> return -1
 
 
     //getting # of sequence (in sequDB) between from and to
-    TString query(Form("SELECT fSequenceFirst FROM Sequences WHERE fSequenceFirst BETWEEN %d and %d", from, to));
+    TString query("SELECT fSequenceFirst FROM Sequences WHERE fSequenceFirst ");
+    query += Form("BETWEEN %d and %d OR fSequenceLast BETWEEN %d and %d",
+                  from, to, from, to);
 
     TSQLResult *res = serv.Query(query);
@@ -331,4 +335,5 @@
             }
         }
+        rc=2;
         break;
 
@@ -351,4 +356,6 @@
 Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
 {
+
+    cout << "Inserting sequence " << from << " ... " << endl;
 
     // ========== Request number of events ==========
@@ -423,6 +430,6 @@
     TString query1("INSERT Sequences SET");
     query1+=Form(" fSequenceFirst=%d,  fSequenceLast=%d,", from, to);
-    query1+=Form(" fProjectKEY=%s,", (*row)[0]);
-    query1+=Form(" fSourceKEY=%s,", (*row)[1]);
+    query1+=Form(" fSourceKEY=%s,", (*row)[0]);
+    query1+=Form(" fProjectKEY=%s,", (*row)[1]);
     query1+=Form(" fNumEvents=%s,", nevts.Data());
     query1+=Form(" fRunTime=%s,", secs.Data());
@@ -451,17 +458,27 @@
     delete res;
 
+
     res = serv.Query(query1);
     if (!res)
-        return kFALSE;
+    {
+        cout << "ERROR - Could not insert Sequence into Sequences." << endl;
+        return kFALSE;
+    }
     delete res;
 
     res = serv.Query(query2);
     if (!res)
-        return kFALSE;
+    {
+        cout << "ERROR - Could not update RunData." << endl;
+        return kFALSE;
+    }
     delete res;
 
     res = serv.Query(query3);
     if (!res)
-        return kFALSE;
+    {
+        cout << "ERROR - Could not insert Sequence into SequenceProcessStatus." << endl;
+        return kFALSE;
+    }
     delete res;
 
@@ -497,6 +514,12 @@
     {
     case 0:
-        cout << " inserting sequence not necessary" << endl;
-        return kTRUE;
+        cout << " sequence not found -> inserting " << from << flush ;
+        if (dummy)
+        {
+            cout << " <dummy> " << endl;
+            return kTRUE;
+        }
+        cout << endl;
+        return InsertSequence(serv, from, to);
 
     case 1:
@@ -508,12 +531,13 @@
         }
         cout << endl;
-        rc = InsertSequence(serv, from, to);
-        if (!rc)
-            cout << "InsertSequence failed!" << endl;
-        break;
+        return InsertSequence(serv, from, to);
+
+    case 2:
+        cout << " sequence is already existing -> inserting not necessary" << endl;
+        return kTRUE;
 
     case -1:
         cout << " deleting went wrong " << endl;
-        rc=kFALSE;
+        return kFALSE;
     }
 
