다음을 통해 공유


방법: 여러 개의 결과 집합 사용

SQL Server Driver for PHP에서는 문의 다음 결과(결과 집합, 행 수 또는 출력 매개 변수)를 활성 상태로 만드는 sqlsrv_next_result 함수를 제공합니다. 이 항목에서는 sqlsrv_next_result를 사용하여 일괄 처리 쿼리의 한 결과에서 다음 결과로 이동하는 방법에 대해 설명합니다. 저장 프로시저에서 반환된 출력 매개 변수를 검색하는 예제는 sqlsrv_next_result를 참조하십시오.

다음 예제에서는 지정된 제품 ID에 대한 제품 검토 정보를 검색하고 해당 제품에 대한 검토를 삽입한 다음 지정된 제품 ID에 대한 제품 검토 정보를 다시 검색하는 일괄 처리 쿼리를 실행합니다. 새로 삽입된 제품 검토는 일괄 처리 쿼리의 최종 결과 집합에 포함됩니다. 이 예제에서는 sqlsrv_next_result를 사용하여 일괄 처리 쿼리의 한 결과에서 다음 결과로 이동합니다.

참고

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

이 예제에서는 AdventureWorks 데이터베이스의 Purchasing.ProductReview 테이블을 사용하며 이 데이터베이스는 서버에 설치되어 있다고 가정합니다. 명령줄에서 이 예제를 실행하면 모든 출력이 콘솔에 기록됩니다.

<?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));
}

/* Define the batch query. */
$tsql = "--Query 1
         SELECT ProductID, ReviewerName, Rating 
         FROM Production.ProductReview 
         WHERE ProductID=?;

         --Query 2
         INSERT INTO Production.ProductReview (ProductID, 
                                               ReviewerName, 
                                               ReviewDate, 
                                               EmailAddress, 
                                               Rating)
         VALUES (?, ?, ?, ?, ?);

         --Query 3
         SELECT ProductID, ReviewerName, Rating 
         FROM Production.ProductReview 
         WHERE ProductID=?;";

/* Assign parameter values and execute the query. */
$params = array(798, 
                798, 
                'CustomerName', 
                '2008-4-15', 
                'test@customer.com', 
                3, 
                798 );
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
     echo "Error in statement execution.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Retrieve and display the first result. */
echo "Query 1 result:\n";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ))
{
     print_r($row);
}

/* Move to the next result of the batch query. */
sqlsrv_next_result($stmt);

/* Display the result of the second query. */
echo "Query 2 result:\n";
echo "Rows Affected: ".sqlsrv_rows_affected($stmt)."\n";

/* Move to the next result of the batch query. */
sqlsrv_next_result($stmt);

/* Retrieve and display the third result. */
echo "Query 3 result:\n";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ))
{
     print_r($row);
}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt );
sqlsrv_close( $conn );
?>

참고 항목

태스크

예제 응용 프로그램

개념

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

관련 자료

데이터 검색
데이터 업데이트(SQL Server Driver for PHP)
API 참조(SQL Server Driver for PHP)