sqlsrv_next_result

지정된 문의 다음 결과(결과 집합, 행 수 또는 출력 매개 변수)를 활성 상태로 만듭니다.

참고

일괄 처리 쿼리 또는 저장 프로시저에서 반환되는 첫 번째 단일 결과는 sqlsrv_next_result를 호출하지 않아도 활성 상태입니다.

구문

sqlsrv_next_result( resource $stmt )

매개 변수

$stmt: 다음 결과를 활성 상태로 만드는 실행된 문입니다.

반환 값

다음 결과가 성공적으로 활성화된 경우 부울 값 true가 반환됩니다. 다음 결과를 활성 상태로 만드는 동안 오류가 발생한 경우 false가 반환됩니다. 사용 가능한 결과가 더 이상 없는 경우 null이 반환됩니다.

다음 예제에서는 제품 검토를 Production.ProductReview 테이블에 삽입하는 저장 프로시저를 만들어 실행한 다음 지정된 제품의 모든 검토를 선택합니다. 이 저장 프로시저가 실행되면 sqlsrv_next_result를 호출하지 않아도 첫 번째 결과(저장 프로시저에 있는 INSERT 쿼리의 영향을 받는 행 수)가 사용됩니다. 두 번째 결과(저장 프로시저의 SELECT 쿼리에서 반환되는 행)는 sqlsrv_next_result를 호출해야 사용 가능하며 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 );
?>

출력 매개 변수가 있는 저장 프로시저를 실행하는 경우 출력 매개 변수의 값에 액세스하기 전에 다른 모든 결과를 사용하는 것이 좋습니다. 자세한 내용은 방법: 매개 변수 방향 지정을 참조하십시오.

참고 항목

개념

설명서에 포함된 코드 예제 정보

관련 자료

API 참조(SQL Server Driver for PHP)