Date
1 - 7 of 7
mssql autoincrement primary key -return value of new record
sherman levine <slevine@...>
I have an MSSQL "transactions" table with an autoincrementing integer primary key field "TransactionNumber".
Magic 9.4 My "add record to transactions table" program is batch modify, no primary table, exit/yes/after with a link create to the transaction file. I can update a parent field (or passed parameter) with the new record's TransactionNumber in the batch task's task suffix (although I can't do so in the record suffix, presumably because TransactionNumber has not yet been assigned by the database.) Is using this method safe and reliable for determining the newly created record's TransactionNumber? I don't really understand why it should work, even though it seems to do so in testing here. Thanks Sherm |
|
"Mike Bannon" <mikeb@...>
Hi Sherm
I'm surprised that it works in the Task Suffix. I would be concerned about its reliability, as the records contents cannot be guaranteed in TS. The way I do it is to just as you are doing, but with a Group Suffix on the primary key, and updating the parameter in there. As you will know, the record's values are always available within the Group Suffix, so this way is definitely reliable. And the reason it works is that when there is an identity field in the dataview (I think as determined by the text 'IDENTITY' in the column's SQL Type property), Magic adds a 'SELECT SCOPE_IDENTITY' to the insert statement, and updates the column in its internal storage with the returned value. Mike Bannon DataFormation Ltd., UK From: magicu-l@... [mailto:magicu-l@...] On Behalf Of sherman levine Sent: 22 July 2013 22:18 To: magic maillist Subject: [magicu-l] mssql autoincrement primary key -return value of new record I have an MSSQL "transactions" table with an autoincrementing integer primary key field "TransactionNumber". Magic 9.4 My "add record to transactions table" program is batch modify, no primary table, exit/yes/after with a link create to the transaction file. I can update a parent field (or passed parameter) with the new record's TransactionNumber in the batch task's task suffix (although I can't do so in the record suffix, presumably because TransactionNumber has not yet been assigned by the database.) Is using this method safe and reliable for determining the newly created record's TransactionNumber? I don't really understand why it should work, even though it seems to do so in testing here. Thanks Sherm |
|
"Andy Jerison" <ajerison@...>
Sherm,
toggle quoted message
Show quoted text
You and I remember the days when the record view was accessible from Task Suffix, but ever since Magic PC 3.0 or so we've had to live without it -- even when examination shows it's really there. In any case, I always use a Group Suffix in these cases, just to be sure the data view will always contain what I think it will. As to why the autoincrement column's value shows up, I imagine that happens when the engine re-reads the row to make sure no other user has updated it. Does that change depending on the lock mode? Andy J -----Original Message-----
From: magicu-l@... [mailto:magicu-l@...] On Behalf Of sherman levine Sent: Monday, July 22, 2013 5:18 PM To: magic maillist Subject: [magicu-l] mssql autoincrement primary key -return value of new record I have an MSSQL "transactions" table with an autoincrementing integer primary key field "TransactionNumber". Magic 9.4 My "add record to transactions table" program is batch modify, no primary table, exit/yes/after with a link create to the transaction file. I can update a parent field (or passed parameter) with the new record's TransactionNumber in the batch task's task suffix (although I can't do so in the record suffix, presumably because TransactionNumber has not yet been assigned by the database.) Is using this method safe and reliable for determining the newly created record's TransactionNumber? I don't really understand why it should work, even though it seems to do so in testing here. Thanks Sherm |
|
"Mike Bannon" <mikeb@...>
Hi Andy
toggle quoted message
Show quoted text
As I said in my reply to Sherm, the identity value is fetched as part of the Insert statement, not as part of a lock, so it is not affected by the locking strategy. Anyway, as this is creating a record there is no locking or re-reading to be done. Mike From: magicu-l@... [mailto:magicu-l@...] On Behalf Of Andy Jerison Sent: 22 July 2013 23:15 To: magicu-l@... Subject: RE: [magicu-l] mssql autoincrement primary key -return value of new record Sherm, You and I remember the days when the record view was accessible from Task Suffix, but ever since Magic PC 3.0 or so we've had to live without it -- even when examination shows it's really there. In any case, I always use a Group Suffix in these cases, just to be sure the data view will always contain what I think it will. As to why the autoincrement column's value shows up, I imagine that happens when the engine re-reads the row to make sure no other user has updated it. Does that change depending on the lock mode? Andy J -----Original Message-----
From: magicu-l@... <mailto:magicu-l%40yahoogroups.com> [mailto:magicu-l@... <mailto:magicu-l%40yahoogroups.com> ] On Behalf Of sherman levine Sent: Monday, July 22, 2013 5:18 PM To: magic maillist Subject: [magicu-l] mssql autoincrement primary key -return value of new record I have an MSSQL "transactions" table with an autoincrementing integer primary key field "TransactionNumber". Magic 9.4 My "add record to transactions table" program is batch modify, no primary table, exit/yes/after with a link create to the transaction file. I can update a parent field (or passed parameter) with the new record's TransactionNumber in the batch task's task suffix (although I can't do so in the record suffix, presumably because TransactionNumber has not yet been assigned by the database.) Is using this method safe and reliable for determining the newly created record's TransactionNumber? I don't really understand why it should work, even though it seems to do so in testing here. Thanks Sherm |
|
sherman levine <slevine@...>
Mike,
toggle quoted message
Show quoted text
Thanks - It does work in the group suffix, using either the primary key or a dummy virtual as the "group" variable. Sherm On 7/22/2013 6:11 PM, Mike Bannon wrote:d Hi Sherm |
|
sherman levine <slevine@...>
Works that way also. Thanks
toggle quoted message
Show quoted text
Sherm On 7/22/2013 6:14 PM, Andy Jerison wrote:
Sherm, |
|
For future reference:
It has to be set as Physical Transaction Mode. Compared to other modes (Deferred, Nested deferred or Within active trans), only Physical can return new Primary key ID. xpa 3.3 and MSSQL 2016 Kenan |
|