Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How do I obtain autoinc key assigned by Oracle
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Divers
Thread ID:
00895405
Message ID:
00895743
Vues:
26
Thanks, Mark. Not having a response this morning, that is exactly what I did. (Great minds think alike, I guess.) I appreciate having my approach confirmed by someone who knows what he is doing!

-- Tom

>The easiest way is to use SQLEXEC to get the next ID and use REPLACE to populate the field in your cursor:
>lnReturn = SQLEXEC(nHandle, 'select sequence_name.nextval AS NextID from dual', 'crsNextID')
>if lnReturn < 0
>   * failed
>   return
>endif
>REPLACE CursorName.IDFieldName with crsNextID.NextID
>
>Then make that IDFieldName also updatable. Your Oracle procedure that populates this column when the record is inserted should look like the following. If it does, then you do not have to worry about "pre-populating" the ID column before the insert occurs:
>create or replace trigger MYTABLE_BEFORE_INSERT
>before insert on schema.MYTABLE for each row
>declare
>v_Id Number;
>BEGIN
> If :new.KeyID Is Null or :new.KeyID < 1 Then
> select sequence_name.nextval into v_Id from dual;
> :new.KeyID := v_Id;
>End If;
>End;
>/
>
>
>>I am working with ODBC back end of Oracle. I used SQLCONNECT for everything. I get a cursor, and set properties to make it updatable, etc. I works very well.
>>
>>On one table, the key field has it value assigned sequentially by Oracle, using a counter -- equivalent to a VFP autoinc field.
>>
>>My question is: once inserted, how do I get the key value back into the record? If I have a cursor with many records -- some modifs, some additions, etc, how do I get the sequential value assigned by Oracle into each record?
>>
>>Someone suggested that I query the counter afterwards. I could do that, but I worry. What if between my addition and the counter query another user has added a record? Can I lock the data base? How do I handle that? Also, is there a nicer or more automatic or more FoxPro-ish was to do this?
>>
>>TIA... Tom
Tom Green in Montreal
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform