Sorry, but I missed that one completely.

The select count(*) exists to decide which of the two inserts to do.

Okay, okay , so I finally compared the two statements in the 'if'. Ther are the same. With the minor exception that one appears to insert the current datetime while the other does not.

Another point - *comment your code*. So you do a count against the table on every single record just to decide whether or not to stick a date into one field? You have to tell us if that is necessary. I have no idea why you would be doing that, so how would I know what to tell you?

Okay, now the rest seems to make a little more sense. You want the results from the cursor SELECT to come back in order of the 3 fields used in the SELECT COUNT(*) statement. For some reason, you want the first record of each set of records to have this datetime filled in. So...

F1 F2 F3 DateField
1 1 1 SYSDATE
1 1 1 NULL
1 1 2 SYSDATE
1 1 2 NULL
1 1 2 NULL
...

Is this correct?

What can we do for this? First of all, you will need to sort your cursor query in order of these fields that you care about. This will then do what you want. Is this the best way? Probably not, but I'd be more interested in whether or not it is truly necessary to do what you are trying to do. If your current SQL actually returns the records in the order you want, you are just plain lucky. If you want records in order, always add the ORDER BY.

Wait, after some more thought - even given that date field value, why do you care about the order? There has to be another field that you care about other than those 3. Just ordering by those 3 fields would be no different than a random order. Basically, there is no more point to me guessing at all this. If you want any more help with this, you need to pony up some more requirements.

- Chris