Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Andy Kramek
  Where is Andy Kramek?
 Westminster Circle, Akron
 Ohio - United States
 Andy Kramek
 To: Rene Relucio
  Where is Rene Relucio?
 Quezon City
 Philippines
 Rene Relucio
 Tags
Subject: RE: Retrieveing autoincremented value by MSSQL to VFP
Thread ID: 99896 Message ID: 99916 # Views: 21 # Ratings: 2
Version: Visual FoxPro 9 Category: General VFP Topics
Date: Wednesday, June 28, 2006 1:00:10 PM         
   


> Hi experts!
>
> At the backend using MSSQL, I have set a field to be primary key that automatically increments its value by 1. Similary with AUTOINC integer type in native foxpro table.
>
> I have one row at the frontend to send data to the backend using remote view, minus of course a data for the primary field key since SQL shall provide the value automatically once it reaches the backend.
>
> After issuing TABLEUPDATE, How do I then retrieve the automatically generated value from the primary key field at the backend so I can display it on the FORM?
>
> I remember time ago, this was one feature to be included in VFP 9.
>
> Is there any other way?
>
> Hope you can help.
>
> TIA
>
> Rene-PCSO


You cannot do this using a remote view. You must use SQL Pass-Through to query SQL Server for the value that it just issued. The basic query is:
lnRes = SQLEXEC( lnCon, "SELECT @@IDENTITY AS lastid", 'cur_lastid' )
IF lnRes > 0
  lnLastID = cur_lastid.lastid
ELSE
  *** SQL Error
ENDIF

Note that @@IDENTITY returns the last Identity value issued on the specified connection! So you must use the same connection as that used by the Remote View in order to get the correct value.

If the table you are updating, in SQL Server, uses triggers to update other tables, then you have to use a different function (IDENT_CURRENT) in order to retrieve the correct value - otherwise the 'last' ID will be for the table updated by the trigger, not the table updated by the view.
lnRes = SQLEXEC( lnCon, "SELECT IDENT_CURRENT( 'table_name' ) AS lastid", 'cur_lastid' )
IF lnRes > 0
  lnLastID = cur_lastid.lastid
ELSE
  *** SQL Error
ENDIF

Regards
Andy Kramek
Microsoft MVP (Visual FoxPro)
Tightline Computers Inc, Akron Ohio, USA



COMPLETE THREAD
Retrieveing autoincremented value by MSSQL to VFP Posted by Rene Relucio @ 6/28/2006 8:05:31 AM
RE: Retrieveing autoincremented value by MSSQL to VFP Posted by Boudewijn Lutgerink @ 6/28/2006 8:21:59 AM
RE: Retrieveing autoincremented value by MSSQL to VFP Posted by Andy Kramek @ 6/28/2006 12:53:31 PM
RE: Retrieveing autoincremented value by MSSQL to VFP Posted by Boudewijn Lutgerink @ 6/28/2006 1:25:38 PM
RE: Retrieveing autoincremented value by MSSQL to VFP Posted by Andy Kramek @ 6/28/2006 1:00:10 PM