Re: Get Last ID Inserted
| From: | David Elliott | Date: | Thu, 21 Nov 2002 17:08:19 +0000 |
| Subject: | Re: Get Last ID Inserted | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-23577@lists.php.net to get a copy of this message | ||
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello Adam
On 21 November 2002 at 11:12:46 -0500 (which was 16:12 where I live) Adam
Voigt rearranged electrons to get
> But if there are heavy operations on the site, will this not also pick
> up a different last inserted id, if in the split milisecond between the
> insert and the next mssql_query which has the @@identity say, another
> user does an insert?
No. It picks up the last identity on that connection. So it does not mater
how many other connections and what they are doing.
It can only be wrong if you close off the connection and open another one.
The other way is to put it all into one SQL statement (as long as you don't
use mssql_????? functions in PHP)
e.g. (one I used earlier)
==========8<=============================================================
begin transaction
set nocount on
declare
@NewId int
update item
set name = 'Canada Delivery'
where itemid = '428'
insert into item
(ItemTypeId, Name, LastUpdated)
values
(49,'Mexico Delivery',getutcdate())
set @NewId = @@identity
insert into DelArea
(itemid,StdEUR, StdGBP, StdUSD, HotEUR, HotGBP, HotUSD)
values
(@NewId,37,19,45,134,70,70)
Update country
set DelId = @NewId
where itemid = 288
commit
select
@NewId NewDelId
==========8<=============================================================
- --
Ti2GO, _______________________________________________
David | David Elliott | Software Engineer |
_________________________| david@elliott.uk.com | PGP Key ID 0x650F4534 |
| Do you think someone was BORGED when they made these up? |
-----BEGIN PGP SIGNATURE-----
Version: 6.5.8ckt http://www.ipgpp.com/
iQA/AwUBPd0TBPmK8eZlD0U0EQL1awCgmS57QwyvS+bz02XsLQtwJOSGSB8AoLmo
I9VdgNIsp3GXkzWAX6I1jYpp
=UXBQ
-----END PGP SIGNATURE-----