隐藏

sqlsrv_cancel

发布:2014/7/8 23:52:13作者:管理员 来源:本站 浏览次数:1230

取消一条语句。这意味着放弃该语句的所有挂起结果。调用此函数之后,如果该语句是使用 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_execute 之后可以使用 sqlsrv_cancel 重新执行。使用 sqlsrv_query 执行的语句在调用sqlsrv_cancel 之后无法重新执行。