On 2015-12-07 22:49, Kent Belan wrote:
Hello,

I am inserting records into an SQL table and I need to find out what is the
Primary Key for the record I just inserted.

GetAutoIncValue() works great for a DBF but not so much for SQL ...

What is the trick to get the PK from an SQL insert  ?

Thanks,
Kent


I think the answers given are for INTEGER autoinc keys. I used to always use those, but I switched over to character keys a couple years ago when rewriting something from years ago to 16-byte character string keys using Craig Boyd's CreateGUID function from VFP2C32.FLL:


        FUNCTION CreateKey(tiLevel as Integer) as String
        * Returns newly created GUID using class from VFP2C32.
                LOCAL lcKey as String
                * 0 = ansi human readable (38 wide)
                * 1 = unicode (76 wide)
                * 2 = binary (16 wide)
                IF VARTYPE(tiLevel) <> "N" THEN
                        tiLevel = this.iDefaultKeyLevel
                ENDIF
IF NOT ("vfp2c32.fll" $ SET("Library")) THEN && mjb 04-16-14 minor tweak. Happy birthday, Bob! :-)
                        SET LIBRARY TO vfp2c32.fll && mjb 05-17-14 took out 
LOCFILE
                ENDIF
                lcKey = CreateGUID(tiLevel)
IF NOT this.ValidKey(lcKey) THEN && try again..don't like those as it caused issue with cboJobType
                        this.TrackBadKey(lcKey)
                        lcKey = this.CreateKey(tiLevel)
                ENDIF
                RETURN lcKey
        ENDFUNC && CreateKey(tiLevel as Integer) as String


This was because I envisioned a multi-user system across multiple networks, specifically a web database but with the option for the user to go "offline" for some processing and hence, autoinc integer keys were not going to be as easy, especially since I have so many multi-level (child/parent/grandparent) table relationships. I do miss the 4-byte width though, when looking at the scale and efficiency as a whole.

--Mike


_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/profox
OT-free version of this list: http://mail.leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message: 
http://leafe.com/archives/byMID/profox/[email protected]
** All postings, unless explicitly stated otherwise, are the opinions of the 
author, and do not constitute legal or medical advice. This statement is added 
to the messages for those lawyers who are too stupid to see the obvious.

Reply via email to