sqlsrv_cancel
取消陳述式。這表示會捨棄陳述式的任何暫止結果。呼叫這個函數之後,如果已經使用 sqlsrv_prepare 將此陳述式準備好,就可以重新執行它。如果已耗用與此陳述式相關的所有結果,便不需要呼叫這個函數。
語法
sqlsrv_cancel( resource $stmt)
參數
$stmt:要取消的陳述式。
傳回值
如果作業已成功則為布林值 true,否則為 false。
範例
下列範例的目標是要讓 AdventureWorks 資料庫執行查詢,然後耗用及計算結果,直到變數 $salesTotal 到達指定的數量為止。然後會捨棄其餘的查詢結果。此範例假設 SQL Server 和 AdventureWorks 資料庫已經安裝在本機電腦上。當從命令列執行此範例時,所有輸出都會寫入主控台。
<?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);
?>
註解
使用 sqlsrv_prepare 和 sqlsrv_execute 組合所準備及執行的陳述式可以在呼叫 sqlsrv_cancel 之後使用 sqlsrv_execute 重新執行。使用 sqlsrv_query 所執行的陳述式在呼叫 sqlsrv_cancel 之後就無法重新執行。