Topic: Insert_ID on mssql -- bug fix
author: Luke Arms
created: 05-11-2007 09:42:56 PM
|
Hi,
Thanks for moving from @@IDENTITY to SCOPE_IDENTITY() in the latest mssql drivers, but unfortunately this isn't a complete solution.
In the _query method of ADODB_mssql et al, queries with parameters are fired using the sp_executesql stored procedure. While this is great for optimisation, it moves any INSERT statements into the scope of the stored procedure, and SCOPE_IDENTITY(), when fired afterwards, returns null (since there have been no inserts in the main scope).
So here's a patch against the 4.96a version of adodb_mssql.inc.php. Presumably similar patches could applied to the other mssql drivers:
====
158a159,161
> if ($this->lastInsID !== false) {
> return $this->lastInsID;
> } else {
159a163
> }
546c550
< return array($sql,$this->qstr($sql2),$max);
---
> return array($sql,$this->qstr($sql2),$max,$sql2);
669a674,679
> $getIdentity = false;
> if (!is_array($sql) && preg_match('/^\\s*insert/i', $sql)) {
> $getIdentity = true;
> $sql .= (preg_match('/;\\s*$/i', $sql) ? ' ' : '; ')
> . $this->identitySQL;
> }
708a719,723
> if ($getIdentity) {
> $arr = @mssql_fetch_row($rez);
> $this->lastInsID = isset($arr[0]) ? $arr[0] : false;
> @mssql_data_seek($rez, 0);
> }
712a728
> $this->lastInsID = false;
715a732
> $this->lastInsID = false; |
|
Topic: Re:Insert_ID on mssql -- bug fix
author: John Lim
created: 06-11-2007 10:34:42 AM
|
Hi, Can you send a copy of the modified source to jlim # natsoft.com
Thanks, John |
|
Topic: Re:Insert_ID on mssql -- bug fix
author: Eloy Lafuente (stronk7)
created: 17-05-2008 09:12:25 PM
|
Hi John,
just to confirm that this is really a big issue, because makes Insert_ID() to fail 100% when using placeholders (executed via sp_executesql).
We have applied Luke's patch against V5.04a and everything seems to be working really better, with Insert_ID() doing its job both in literal and explicit inserts.
The patch for V5.04a is available at: http ://tracker.moodle.org/browse/MDL-14886
Hope this helps, thanks Luke!
Ciao :-) |
|
Topic: Re:Insert_ID on mssql -- bug fix
author: John Lim
created: 12-06-2008 04:35:36 AM
|
Thanks. Added. |
|
|