diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php index 003ae80a90f..7b9e9f0ad3c 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php @@ -40,6 +40,21 @@ public function __construct($dsn, $user = null, $password = null, array $options /** * @override */ + 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} + */ public function quote($value, $type=\PDO::PARAM_STR) { $val = parent::quote($value, $type); diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php index 514ae54fc54..9a80b68d235 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php @@ -130,9 +130,8 @@ public function exec($statement) 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(); }