http://msdn.microsoft.com/zh-cn/library/cc296162(v=SQL.90).aspx
| 当多次执行预定义语句并为每次执行使用不同的参数值时,此函数非常理想。有关详细信息,请参阅如何多次执行一个查询。
|
以下示例执行用于更新 AdventureWorks 数据库中 Sales.SalesOrderDetail 表中的某字段的语句。此示例假定本地计算机上已安装 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));
}
/* Set up the Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty = ( ?)
WHERE SalesOrderDetailID = ( ?)";
/* Set up the parameters array. Parameters correspond, in order, to
question marks in $tsql. */
$params = array( 5, 10);
/* Create the statement. */
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
if( $stmt )
{
echo "Statement prepared.\n";
}
else
{
echo "Error in preparing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.\n";
}
else
{
echo "Error in executing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
本文介绍如何在PHP中使用sqlsrv_execute函数执行SQL Server中的预定义语句,并通过一个更新AdventureWorks数据库中Sales.SalesOrderDetail表的具体示例进行演示。
8904

被折叠的 条评论
为什么被折叠?



