The sqlsrv driver has no native function to retrieve the inserted log id. In this case you need to send two queries at once only INSERT
and the second one SELECT
at scope_identity which will return the value of the inserted identity field.
sqlsrv_next_result returns true if something exists (resultsets, number of affected rows, or even another output) in the concerned sql statement that is stored in the $stmt
variable. At each call of sqlsrv_next_result()
a sql is unstacked to get its return use sqlsrv_fetch_*
The code should look like this:
$sql = "INSERT INTO [RDO].[dbo].[ANALISE_CRITICA]
(CC, NMANALISE, TXTOBS, VLRECEITACONT, VLRECEITABRUTA)
VALUES (?,?,?,?,?); SELECT SCOPE_IDENTITY() AS last_id";
$params = array($cc, $analiseCritica, $objetoExtra,
$receitaContrato, $receitaBrutaMarco);
$stmt = sqlsrv_query($conn, $sql, $params); //processa a consulta
$next_result = sqlsrv_next_result($stmt); //em caso sucesso desempilha a proxima sql(select)
if($next_result){
$item = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC); //obtem o resultado
echo $item['last_id'];
}
Note: avoid using @
in the code they mask the error messages and make it difficult to detect the problem, treat the error.
Based on:
p>
manual - sqlsrv_next_result