Export (0) Print
Expand All

sqlsrv_cancel

SQL Server 2008 R2

Cancels a statement. This means that any pending results for the statement are discarded. After this function is called, the statement can be re-executed if it was prepared with sqlsrv_prepare. Calling this function is not necessary if all the results associated with the statement have been consumed.

sqlsrv_cancel( resource $stmt)

$stmt: The statement to be canceled.

A Boolean value: true if the operation was successful. Otherwise, false.

The following example targets the AdventureWorks database to execute a query, then consumes and counts results until the variable $salesTotal reaches a specified amount. The remaining query results are then discarded. The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. All output is written to the console when the example is run from the command line.

<?php
/* Connect to the local server using Windows Authentication and 
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
     echo "Could not connect.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Prepare and execute the query. */
$tsql = "SELECT OrderQty, UnitPrice FROM Sales.SalesOrderDetail";
$stmt = sqlsrv_prepare( $conn, $tsql);
if( $stmt === false )
{
     echo "Error in statement preparation.\n";
     die( print_r( sqlsrv_errors(), true));
}
if( sqlsrv_execute( $stmt ) === false)
{
     echo "Error in statement execution.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Initialize tracking variables. */
$salesTotal = 0;
$count = 0;

/* Count and display the number of sales that produce revenue
of $100,000. */
while( ($row = sqlsrv_fetch_array( $stmt)) && $salesTotal <=100000)
{
     $qty = $row[0];
     $price = $row[1];
     $salesTotal += ( $price * $qty);
     $count++;
}
echo "$count sales accounted for the first $$salesTotal in revenue.\n";

/* Cancel the pending results. The statement can be reused. */
sqlsrv_cancel( $stmt);
?>

A statement that is prepared and executed using the combination of sqlsrv_prepare and sqlsrv_execute can be re-executed with sqlsrv_execute after calling sqlsrv_cancel. A statement that is executed with sqlsrv_query cannot be re-executed after calling sqlsrv_cancel.

Community Additions

ADD
Show:
© 2014 Microsoft