다음을 통해 공유


방법: 입/출력 매개 변수 검색

이 항목에서는 하나의 매개 변수가 입/출력 매개 변수로 정의된 저장 프로시저를 호출하고 결과를 검색하는 방법을 보여 줍니다. 출력 매개 변수나 입/출력 매개 변수를 검색할 때 저장 프로시저에서 반환된 모든 결과를 사용해야만 반환된 매개 변수 값에 액세스할 수 있습니다.

참고

null로 초기화되거나 업데이트된 변수, DateTime 또는 스트림 형식은 출력 매개 변수로 사용할 수 없습니다.

다음 예제에서는 지정된 직원의 사용할 수 있는 휴가 시간에서 사용한 휴가 시간을 빼는 저장 프로시저를 호출합니다. 사용한 휴가 시간을 나타내는 변수인 $vacationHrs는 저장 프로시저에 입력 매개 변수로 전달됩니다. 저장 프로시저에서는 사용할 수 있는 휴가 시간을 업데이트한 후 같은 매개 변수를 사용하여 나머지 휴가 시간을 반환합니다.

참고

$vacationHrs를 4로 초기화하면 반환되는 PHPTYPE이 정수로 설정됩니다. 데이터 형식 무결성을 보장하려면 저장 프로시저를 호출하기 전에 입/출력 매개 변수를 초기화하거나 원하는 PHPTYPE을 지정해야 합니다. PHPTYPE을 지정하는 방법은 방법: PHP 데이터 형식 지정을 참조하십시오.

저장 프로시저는 두 개의 결과를 반환하므로 저장 프로시저를 실행한 후 sqlsrv_next_result를 호출하여 출력 매개 변수 값을 사용할 수 있도록 해야 합니다. sqlsrv_next_result를 호출한 후 $vacationHrs에는 저장 프로시저에서 반환된 출력 매개 변수 값이 포함됩니다.

참고

정식 구문을 사용하여 저장 프로시저를 호출하는 것이 좋습니다. 정식 구문에 대한 자세한 내용은 저장 프로시저 호출(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('SubtractVacationHours', 'P') IS NOT NULL
                DROP PROCEDURE SubtractVacationHours";
$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 SubtractVacationHours
                        @EmployeeID int,
                        @VacationHrs smallint OUTPUT
                  AS
                  UPDATE HumanResources.Employee
                  SET VacationHours = VacationHours - @VacationHrs
                  WHERE EmployeeID = @EmployeeID;
                  SET @VacationHrs = (SELECT VacationHours
                                      FROM HumanResources.Employee
                                      WHERE EmployeeID = @EmployeeID)";

$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 SubtractVacationHours( ?, ?)}";

/* Define the parameter array. By default, the first parameter is an
INPUT parameter. The second parameter is specified as an INOUT
parameter. Initializing $vacationHrs to 8 sets the returned PHPTYPE to
integer. To ensure data type integrity, output parameters should be
initialized before calling the stored procedure, or the desired
PHPTYPE should be specified in the $params array.*/

$employeeId = 4;
$vacationHrs = 8;
$params = array( 
                 array($employeeId, SQLSRV_PARAM_IN),
                 array($vacationHrs, SQLSRV_PARAM_INOUT)
               );

/* 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));
}

/* Display the value of the output parameter $vacationHrs. */
sqlsrv_next_result($stmt3);
echo "Remaining vacation hours: ".$vacationHrs;

/*Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_close( $conn);
?>

참고 항목

태스크

방법: 매개 변수 방향 지정
방법: 출력 매개 변수 검색

관련 자료

데이터 검색