I am creating a website using PHP and Zend Framework. I am uisng the sqlsrv adaptor to talk to my database. I am trying to write an insert query and also store the ID of the new row. I have written an SQL query like this:
$db = Zend_Db_Table::getDefaultAdapter();
$id = $db->fetchOne("
INSERT INTO MyTable
(Title, CreatedBy, Created, LastUpdatedBy, LastUpdated)
SELECT 'MY Title',
UserTable.UserId,
CONVERT(datetime, '".$this->details["Created"]."', 120),
UserTable.UserId,
CONVERT(datetime, '".$this->details["LastUpdated"]."', 120)
FROM UserTable
WHERE UserName = '".$this->details["CreatedBy"]."'
SELECT CAST(SCOPE_IDENTITY() AS int) AS Id"
);
but it generates this error:
'Zend_Db_Statement_Sqlsrv_Exception' with message 'The active result for the query contains no fields.
How can I avoid this error and run this query and retreive the inserted row Id?