is this a bug in vfp9?

Hi, i am using VFP9 on win98,XP & W2k.

i use following code to obtained new prn_no. i.e. if all prn_no are in sqeuence, code will return last prn_no + 1, and if any record is missed, then that missed no. will be returened.

select prn_no from sm_student_master where section_id = 1 order by prn_no into cursor cursor_temp

locate for prn_no <> recno()

if found()

ln_prn_no = recno()

else

ln_prn_no = _tally + 1

endif

If i use this code, sometime invalid ln_prn_no returned. and when i add readwrite method in select -sql command, it will returned correct prn_no always. so it is necessory to add readwrite keyword with each select-sql command

Javid




Answer this question

is this a bug in vfp9?

  • Exentrick1

    Sorry, i use flock("sm_student_master") before running this query. so no duplication possible. i can maintain NEXT_ID in a separate table but it always return next number. in my project, it is compulsory to reuse missing prn_no.

  • btb73

    i can maintain NEXT_ID in a separate table but it always return next number. in my project, it is compulsory to reuse missing prn_no.

    My first thought is that if these numbers cannot have any gaps, then they are numbers like Invoice Numbers and, therefore, have business meaning. If this is the case, they really should not be used a primary keys. The only function of a primary key is to uniquely identify the record.

    However, if you still want to use this field as a primary key and have no gaps in the sequence, you can still use code similar to the NextID function illustrated in the TasTrade sample application. The only difference if that you cannot use this function as the default value for the field. Instead, you will need to explicitly call the function right before you save the newly added record. There is still a very very small chance that the save would fail and you would then have a gap in the sequence, but on a newly appended record, this chance is extremely tiny.



  • UFAnders

    This is NOT a good way to get a new id. If two users run this code at the same time, they might both get the same value.

    In general, there's no reason to worry about skipped id numbers. Just ignore them.

    Since you're using VFP 9, the easiest way to get id numbers is to set the field in question to autoincrement. If you can't do that for some reason, then the right way to do this is to maintain a separate table containing the next available id. When you need a new one, you lock that record, grab the value, increment it, and unlock. You'll find an example of code to do this in the TasTrade database that comes with VFP--look for the NewID stored procedure.

    Tamar

  • GhostlyDeath

    Intel4,

    As Tamar and Marcia have said, invoice numbers do not make good Primary Keys. PKs should be meaningless surrogate keys. Autoinc fields are good for that.

    If you want to generate invoice numbers from a counter you can look at this sample code:

    http://feldstein.net/newid.asp

     

    If you need more clarification on the meaning and use of Surrogate Keys, look here:

    http://fox.wikis.com/wc.dll Wiki~SurrogateKey

    http://fox.wikis.com/wc.dll Wiki~PrimaryKey

     


  • is this a bug in vfp9?