Commit cc1e9a90 authored by Marco Pivetta's avatar Marco Pivetta Committed by GitHub

Merge pull request #2634 from deeky666/fix-sqlserver-sequence-last-insert-id

Fix fetching last insert ID for sequences on SQL Server
parents 42330514 55338541
......@@ -37,6 +37,21 @@ class Connection extends PDOConnection implements \Doctrine\DBAL\Driver\Connecti
$this->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array(Statement::class, array()));
}
/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
{
if (null === $name) {
return parent::lastInsertId($name);
}
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt->execute(array($name));
return $stmt->fetchColumn();
}
/**
* {@inheritDoc}
*/
......
......@@ -130,9 +130,8 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
public function lastInsertId($name = null)
{
if ($name !== null) {
$sql = "SELECT IDENT_CURRENT(".$this->quote($name).") AS LastInsertId";
$stmt = $this->prepare($sql);
$stmt->execute();
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt->execute(array($name));
return $stmt->fetchColumn();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment