Skip to content
Permalink
Browse files

Fix MSSQL Insert_ID() to work when inserting using placeholders. MDL-…

…14886
  • Loading branch information...
stronk7
stronk7 committed May 18, 2008
1 parent d02e368 commit f4607d530975f0bdde7c4b4ac66a92636bcf6be9
Showing with 18 additions and 2 deletions.
  1. +18 −2 lib/adodb/drivers/adodb-mssql.inc.php
@@ -152,7 +152,11 @@ function _insertid()
// the same scope. A scope is a module -- a stored procedure, trigger,
// function, or batch. Thus, two statements are in the same scope if
// they are in the same stored procedure, function, or batch.
if ($this->lastInsID !== false) {
return $this->lastInsID; // InsID from sp_executesql call
} else {
return $this->GetOne($this->identitySQL);
}
}
function _affectedrows()
@@ -539,7 +543,7 @@ function Prepare($sql)
for ($i = 1, $max = sizeof($sqlarr); $i < $max; $i++) {
$sql2 .= '@P'.($i-1) . $sqlarr[$i];
}
return array($sql,$this->qstr($sql2),$max);
return array($sql,$this->qstr($sql2),$max,$sql2);
}
function PrepareSP($sql)
@@ -663,6 +667,11 @@ function _query($sql,$inputarr)
# bind input params with sp_executesql:
# see http://www.quest-pipelines.com/newsletter-v3/0402_F.htm
# works only with sql server 7 and newer
$getIdentity = false;
if (!is_array($sql) && preg_match('/^\\s*insert/i', $sql)) {
$getIdentity = true;
$sql .= (preg_match('/;\\s*$/i', $sql) ? ' ' : '; ') . $this->identitySQL;
}
if (!is_array($sql)) $sql = $this->Prepare($sql);
$params = '';
$decl = '';
@@ -702,13 +711,20 @@ function _query($sql,$inputarr)
$decl = $this->qstr($decl);
if ($this->debug) ADOConnection::outp("<font size=-1>sp_executesql N{$sql[1]},N$decl,$params</font>");
$rez = mssql_query("sp_executesql N{$sql[1]},N$decl,$params", $this->_connectionID);
if ($getIdentity) {
$arr = @mssql_fetch_row($rez);
$this->lastInsID = isset($arr[0]) ? $arr[0] : false;
@mssql_data_seek($rez, 0);
}
} else if (is_array($sql)) {
# PrepareSP()
$rez = mssql_execute($sql[1]);
$this->lastInsID = false;
} else {
$rez = mssql_query($sql,$this->_connectionID);
$this->lastInsID = false;
}
return $rez;
}
@@ -1035,4 +1051,4 @@ static function UnixTimeStamp($v)
http://www.databasejournal.com/scripts/article.php/1440551
*/
?>
?>

0 comments on commit f4607d5

Please sign in to comment.
You can’t perform that action at this time.