sqlsrv_cancel

PHP ドライバーのダウンロード

ステートメントをキャンセルします。 これは、保留中のステートメントの結果を破棄することを意味します。 この関数が呼び出されると、ステートメントを再実行することができます (ステートメントが 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_preparesqlsrv_execute の組み合わせを使用して準備され実行されたステートメントは、sqlsrv_cancel を呼び出した後、sqlsrv_execute を使用して再実行することができます。 sqlsrv_query を使用して実行されたステートメントは、sqlsrv_cancel の呼び出し後に再実行することはできません。

参照

SQLSRV ドライバー API リファレンス

サーバーへの接続

データの取得

ドキュメントのコード例について

sqlsrv_free_stmt