이 항목에서는 하나의 매개 변수가 출력 매개 변수로 정의된 저장 프로시저를 호출하는 방법에 대해 설명합니다. 출력 매개 변수나 입/출력 매개 변수를 검색할 때 저장 프로시저에서 반환된 모든 결과를 사용해야만 반환된 매개 변수 값에 액세스할 수 있습니다.
참고
null로 초기화되거나 업데이트된 변수, DateTime 또는 스트림 형식은 출력 매개 변수로 사용할 수 없습니다.
예
다음 예제에서는 지정된 직원에 대한 연간 판매 실적을 반환하는 저장 프로시저를 호출합니다. PHP 변수 $lastName은 입력 매개 변수이고 $salesYTD는 출력 매개 변수입니다.
참고
$salesYTD를 0.0으로 초기화하면 반환되는 PHPTYPE이 float로 설정됩니다. 데이터 형식 무결성을 보장하려면 저장 프로시저를 호출하기 전에 출력 매개 변수를 초기화하거나 원하는 PHPTYPE을 지정해야 합니다. PHPTYPE 지정 방법은 방법: PHP 데이터 형식 지정을 참조하십시오.
저장 프로시저에서는 한 개의 결과만 반환하므로 저장 프로시저가 실행된 직후에 $salesYTD에는 반환된 출력 매개 변수 값이 포함됩니다.
참고
저장 프로시저를 호출할 때는 정식 구문을 사용하는 것이 좋습니다. 정식 구문에 대한 자세한 내용은 저장 프로시저 호출(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('GetEmployeeSalesYTD', 'P') IS NOT NULL
DROP PROCEDURE GetEmployeeSalesYTD";
$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 GetEmployeeSalesYTD
@SalesPerson nvarchar(50),
@SalesYTD money OUTPUT
AS
SELECT @SalesYTD = SalesYTD
FROM Sales.SalesPerson AS sp
JOIN HumanResources.vEmployee AS e
ON e.EmployeeID = sp.SalesPersonID
WHERE LastName = @SalesPerson";
$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 GetEmployeeSalesYTD( ?, ? )}";
/* Define the parameter array. By default, the first parameter is an
INPUT parameter. The second parameter is specified as an OUTPUT
parameter. Initializing $salesYTD to 0.0 sets the returned PHPTYPE to
float. 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.*/
$lastName = "Blythe";
$salesYTD = 0.0;
$params = array(
array($lastName, SQLSRV_PARAM_IN),
array($salesYTD, SQLSRV_PARAM_OUT)
);
/* 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 $salesYTD. */
echo "YTD sales for ".$lastName." are ". $salesYTD. ".";
/*Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt1);
sqlsrv_free_stmt( $stmt2);
sqlsrv_free_stmt( $stmt3);
sqlsrv_close( $conn);
?>
참고 항목
태스크
방법: 매개 변수 방향 지정
방법: 입/출력 매개 변수 검색