Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » ElevateDB Technical Support » Support Forums » ElevateDB SQL » View Thread |
Messages 11 to 18 of 18 total |
Best way to store and increment integer primary index values |
Thu, Dec 3 2009 6:58 AM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. timyoung@elevatesoft.com | |
Tue, Dec 8 2009 6:55 AM | Permanent Link |
Peter | Tim
<<<<< The best (and easiest) way is to do as Roy suggests, and define your primary key column as an IDENTITY column. You can return IDENTITY columns as output parameters from INSERT statements, so you can always easily retrieve the value generated during the INSERT: http://www.elevatesoft.com/newsgrp?action=openmsg&group=16&msg=8697&page=1#msg8697 >>>> How do I get the value of the new ID field back from the script? I would like to use something like this... function AddRecdGetID(const SSQL: string): integer; const SCRADDID = 'SCRIPT (OUT LastIdentity INTEGER)'+ ' BEGIN'+ ' DECLARE TempStmt STATEMENT;'+ ' PREPARE TempStmt FROM ''INSERT INTO "Address" (LocationID, ClientID) VALUES (?, ?)'' ;'+ ' EXECUTE TempStmt USING LastIdentity, 444;'+ ' END'; var aScript: TEDBScript; begin Result := -1; aScript := TEDBScript.Create(nil); try aScript.SessionName := DM.EDBDatabase1.SessionName; aScript.DatabaseName := DM.EDBDatabase1.DatabaseName; aScript.SQL.Add(SCRADDID); aScript.Prepare; try aScript.ExecScript; except raise; end; Result := aScript.FieldByName('AddressID').AsInteger;<<<<<<<<<<<<<<<<< finally aScript.Free; end; end; ...but of course there is no field by that name. It IS in the table, which has AddressID, LocationID and ClientID integer fields. The SQL works, and the AddressID field is incremented, but I would like it to return the value of the new AddressID as the resultof the AddRecdGetID function. Thanks for your help Regards Peter |
Tue, Dec 8 2009 8:47 AM | Permanent Link |
Roy Lambert NLH Associates Team Elevate | Peter
Check this out http://www.elevatesoft.com/newsgrp?action=openmsg&group=17&msg=2164&page=1#msg2164 Roy Lambert [Team Elevate] |
Tue, Dec 8 2009 3:58 PM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. timyoung@elevatesoft.com | Peter,
<< How do I get the value of the new ID field back from the script? >> Use this: function AddRecdGetID(const SSQL: string): integer; const SCRADDID = 'SCRIPT (OUT LastIdentity INTEGER)'+ ' BEGIN'+ ' DECLARE TempStmt STATEMENT;'+ ' PREPARE TempStmt FROM ''INSERT INTO "Address" (LocationID, ClientID) VALUES (?, ?)'' ;'+ ' EXECUTE TempStmt USING LastIdentity, 444;'+ ' END'; var aScript: TEDBScript; begin Result := -1; aScript := TEDBScript.Create(nil); try aScript.SessionName := DM.EDBDatabase1.SessionName; aScript.DatabaseName := DM.EDBDatabase1.DatabaseName; aScript.SQL.Add(SCRADDID); aScript.Prepare; try aScript.ExecScript; Result := aScript.ParamByName('LastIdentity').AsInteger;<<<<<<<<<<<<<<<<< except raise; end; finally aScript.Free; end; end; -- Tim Young Elevate Software www.elevatesoft.com |
Tue, Dec 8 2009 7:10 PM | Permanent Link |
Peter | Tim
The Parameter LastIdentity is always NULL, after the aScript.ExecScript. The row is added to the table just fine, but the resulting new ID is elusive. Should the LastIdentity Param be formally created? I am using 2.03 in D7. Regards Peter |
Wed, Dec 9 2009 3:23 PM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. timyoung@elevatesoft.com | Peter,
<< The Parameter LastIdentity is always NULL, after the aScript.ExecScript. The row is added to the table just fine, but the resulting new ID is elusive. Should the LastIdentity Param be formally created? >> No, it will be created automatically. I just noticed that you're trying to return the AddressID as the IDENTITY column, but you're specifying the OUT parameter in the script for the LocationID column. Is that incorrect ? -- Tim Young Elevate Software www.elevatesoft.com |
Wed, Dec 9 2009 8:25 PM | Permanent Link |
Peter | Tim
<<<< I just noticed that you're trying to return the AddressID as the IDENTITY column, but you're specifying the OUT parameter in the script for the LocationID column. Is that incorrect ?>>>>>> No, My intention is the have the new AddressID returned. Regards Peter |
Thu, Dec 10 2009 2:53 PM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. timyoung@elevatesoft.com | Peter,
<< No, My intention is the have the new AddressID returned. >> I understand, but you're referring to the LocationID column in the INSERT statement, hence your problem. What you need is this instead: SCRIPT (OUT LastIdentity INTEGER) BEGIN DECLARE TempStmt STATEMENT; PREPARE TempStmt FROM 'INSERT INTO "Address" (AddressID,ClientID) VALUES (?, ?)'; EXECUTE TempStmt USING LastIdentity, 444; END Notice the use of AddressID instead of LocationID in the INSERT. -- Tim Young Elevate Software www.elevatesoft.com |
« Previous Page | Page 2 of 2 | |
Jump to Page: 1 2 |
This web page was last updated on Saturday, May 4, 2024 at 12:54 AM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |