使指定语句的下一个结果(结果集、行计数或输出参数)处于活动状态。
| 注意: |
|---|
|
无需调用 sqlsrv_next_result,批处理查询或存储过程返回的第一个(或唯一的)结果就已处于活动状态。
|
sqlsrv_next_result( resource $stmt )
$stmt:执行的使下一个结果处于活动状态的语句。
如果已成功使下一个结果处于活动状态,则返回布尔值 true。如果在使下一个结果处于活动状态的过程中发生错误,则返回 false。如果没有其他任何结果可用,则返回 null。
以下示例创建和执行一个存储过程,此存储过程将一个产品审核插入 Production.ProductReview 表,然后选择指定产品的所有审核。在执行此存储过程之后,在不调用sqlsrv_next_result 的情况下使用第一个结果(受此存储过程中的 INSERT 查询影响的行数)。通过调用 sqlsrv_next_result 使下一个结果(由此存储过程中的 SELECT 查询返回的行)可用,并通过使用 sqlsrv_fetch_array 来使用此结果。
| 注意: |
|---|
|
建议使用规范语法来调用存储过程。有关规范语法的详细信息,请参阅 Calling a Stored Procedure(调用存储过程)。
|
此示例假定本地计算机上已安装了 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));
}
/* Drop the stored procedure if it already exists. */
$tsql_dropSP = "IF OBJECT_ID('InsertProductReview', 'P') IS NOT NULL
DROP PROCEDURE InsertProductReview";
$stmt1 = sqlsrv_query( $conn, $tsql_dropSP);
if( $stmt1 === false )
{
echo "Error in executing statement 1.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Create the stored procedure. */
$tsql_createSP = " CREATE PROCEDURE InsertProductReview
@ProductID int,
@ReviewerName nvarchar(50),
@ReviewDate datetime,
@EmailAddress nvarchar(50),
@Rating int,
@Comments nvarchar(3850)
AS
BEGIN
INSERT INTO Production.ProductReview
(ProductID,
ReviewerName,
ReviewDate,
EmailAddress,
Rating,
Comments)
VALUES
(@ProductID,
@ReviewerName,
@ReviewDate,
@EmailAddress,
@Rating,
@Comments);
SELECT * FROM Production.ProductReview
WHERE ProductID = @ProductID;
END";
$stmt2 = sqlsrv_query( $conn, $tsql_createSP);
if( $stmt2 === false)
{
echo "Error in executing statement 2.\n";
die( print_r( sqlsrv_errors(), true));
}
/*-------- The next few steps call the stored procedure. --------*/
/* Define the Transact-SQL query. Use question marks (?) in place of the
parameters to be passed to the stored procedure */
$tsql_callSP = "{call InsertProductReview(?, ?, ?, ?, ?, ?)}";
/* Define the parameter array. */
$productID = 709;
$reviewerName = "Customer Name";
$reviewDate = "2008-02-12";
$emailAddress = "customer@email.com";
$rating = 3;
$comments = "[Insert comments here.]";
$params = array(
$productID,
$reviewerName,
$reviewDate,
$emailAddress,
$rating,
$comments
);
/* Execute the query. */
$stmt3 = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt3 === false)
{
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Consume the first result (rows affected by INSERT query in the
stored procedure) without calling sqlsrv_next_result. */
echo "Rows affectd: ".sqlsrv_rows_affected($stmt3)."-----\n";
/* Move to the next result and display results. */
$next_result = sqlsrv_next_result($stmt3);
if( $next_result )
{
echo "\nReview information for product ID ".$productID.".---\n";
while( $row = sqlsrv_fetch_array( $stmt3, SQLSRV_FETCH_ASSOC))
{
echo "ReviewerName: ".$row['ReviewerName']."\n";
echo "ReviewDate: ".date_format($row['ReviewDate'],
"M j, Y")."\n";
echo "EmailAddress: ".$row['EmailAddress']."\n";
echo "Rating: ".$row['Rating']."\n\n";
}
}
elseif( is_null($next_result))
{
echo "No more results.\n";
}
else
{
echo "Error in moving to next result.\n";
die(print_r(sqlsrv_errors(), true));
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt1 );
sqlsrv_free_stmt( $stmt2 );
sqlsrv_free_stmt( $stmt3 );
sqlsrv_close( $conn );
?>
在执行具有输出参数的存储过程时,建议先使用所有其他结果,然后再访问输出参数的值。有关详细信息,请参阅如何指定参数方向。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有