Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get the last identity value for a table?
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01421835
Message ID:
01422128
Views:
58
David,

Let me explain more of what I am doing and why scope_identity() may not be applicable in my case.

I have a generic routine where an array of one or more (easily could be 100) rows is created. Each row of the array has an expression, either UPDATE or INSERT. The method that receives this array scan through the rows and executes each command (being UPDATE or INSERT). (And of course I use TRANSACTION so that if any one command failes it will be ROLLed back). Some of the UPDATE commands need to have a value of IDENTITY (PK) from some other INSERT command. So for these cases the UPDATE expression has a "place holder" string in a form of a call to a method that gets the IDENTIFY from a different table and then the program replaces/inserts this value into the UPDATE expression. Since there is difficult to predict in which order the rows of the array will be processed (that is, there could be an INSERT between INSERT that I need to get the IDENTITY value and the UPDATE where this value is necessary, I am choosing to use INDENT_CURRENT which gives me the value. I have not tested the entire routine yet; will be doing it this week.

And at this time I do not want to use Stored Procedures. Maybe in the next or next update I will though.

Thank you for your input.

>Dmitry,
>
>To make sure you understand.. pretty much by the time your VFP code gets the max(PKField) value back to it, the value returned can be (read that as WILL BE) out of date.
>
>If you intend to know the PK value that got used to create a record and you don't want to use an SP you'll have to write a block of code like this
>
>
>insert into blah...
>select scope_identity() as PKCreated
>
>
>Then back in VFP you can use the returned cursor.PKCreated field for child table inserts to link the records to the parent table you just inserted into.
>
>In the long run you'll be better off doing stored procs for this.
>
>>I realize that. But, in the first version of my VFP with SQL Server I am trying to stay away from stored procedures. It will probably be in one of the refactoring versions.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform