使用 php 中的输入和输出参数调用 SQL 服务器存储过程
Call SQL Server Stored procedure with input and output parameter in php
我想在单个存储过程下 运行 10 select 查询,并使用输出参数分别获取每个 select 查询的结果,并使用 php 和 sqlsrv 库。如何获得?
我不知道如何在 PHP 中执行此操作,但您通常可以用结果集替换输出参数,这不需要任何特殊的客户端 API。
EG
declare @count1 int = (select count(*) from sys.objects)
declare @count2 int = (select count(*) from sys.columns)
select *
from (values ('count1', @count1),('count2', @count2) ) as t(pn,pv)
这是一个基本但有效的示例。
数据库部分:
USE [TestDatabase]
CREATE PROCEDURE [TestProcedure]
@P1 int OUTPUT,
@P2 int OUTPUT,
@P3 int OUTPUT,
@P4 int OUTPUT,
@P5 int OUTPUT,
@P6 int OUTPUT,
@P7 int OUTPUT,
@P8 int OUTPUT,
@P9 int OUTPUT,
@P10 int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- It's very basic example.
-- Put here your own SQL statements, which must return single values
SELECT @P1 = 1
SELECT @P2 = 2
SELECT @P3 = 3
SELECT @P4 = 4
SELECT @P5 = 5
SELECT @P6 = 6
SELECT @P7 = 7
SELECT @P8 = 8
SELECT @P9 = 9
SELECT @P10 = 10
END
PHP 部分:
<?php
// Connection
$server = 'server\instance,port';
$cinfo = array(
"Database"=>'TestDatabase',
"UID"=>"User",
"PWD"=>"Password"
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
// Call stored procedure
$sql = '{call TestProcedure(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}';
$p1 = 0;
$p2 = 0;
$p3 = 0;
$p4 = 0;
$p5 = 0;
$p6 = 0;
$p7 = 0;
$p8 = 0;
$p9 = 0;
$p10 = 0;
$params = array(
array(&$p1, SQLSRV_PARAM_INOUT),
array(&$p2, SQLSRV_PARAM_INOUT),
array(&$p3, SQLSRV_PARAM_INOUT),
array(&$p4, SQLSRV_PARAM_INOUT),
array(&$p5, SQLSRV_PARAM_INOUT),
array(&$p6, SQLSRV_PARAM_INOUT),
array(&$p7, SQLSRV_PARAM_INOUT),
array(&$p8, SQLSRV_PARAM_INOUT),
array(&$p9, SQLSRV_PARAM_INOUT),
array(&$p10, SQLSRV_PARAM_INOUT)
);
$stmt = sqlsrv_query($conn, $sql, $params);
if( $stmt === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
// Output parameters are available after consuming all resultsets.
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
// Output
echo 'Parameters values'."</br>";
echo 'Parameter 1: '.$p1."</br>";
echo 'Parameter 2: '.$p2."</br>";
echo 'Parameter 3: '.$p3."</br>";
echo 'Parameter 4: '.$p4."</br>";
echo 'Parameter 5: '.$p5."</br>";
echo 'Parameter 6: '.$p6."</br>";
echo 'Parameter 7: '.$p7."</br>";
echo 'Parameter 8: '.$p8."</br>";
echo 'Parameter 9: '.$p9."</br>";
echo 'Parameter 10: '.$p10."</br>";
?>
我想在单个存储过程下 运行 10 select 查询,并使用输出参数分别获取每个 select 查询的结果,并使用 php 和 sqlsrv 库。如何获得?
我不知道如何在 PHP 中执行此操作,但您通常可以用结果集替换输出参数,这不需要任何特殊的客户端 API。
EG
declare @count1 int = (select count(*) from sys.objects)
declare @count2 int = (select count(*) from sys.columns)
select *
from (values ('count1', @count1),('count2', @count2) ) as t(pn,pv)
这是一个基本但有效的示例。
数据库部分:
USE [TestDatabase]
CREATE PROCEDURE [TestProcedure]
@P1 int OUTPUT,
@P2 int OUTPUT,
@P3 int OUTPUT,
@P4 int OUTPUT,
@P5 int OUTPUT,
@P6 int OUTPUT,
@P7 int OUTPUT,
@P8 int OUTPUT,
@P9 int OUTPUT,
@P10 int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- It's very basic example.
-- Put here your own SQL statements, which must return single values
SELECT @P1 = 1
SELECT @P2 = 2
SELECT @P3 = 3
SELECT @P4 = 4
SELECT @P5 = 5
SELECT @P6 = 6
SELECT @P7 = 7
SELECT @P8 = 8
SELECT @P9 = 9
SELECT @P10 = 10
END
PHP 部分:
<?php
// Connection
$server = 'server\instance,port';
$cinfo = array(
"Database"=>'TestDatabase',
"UID"=>"User",
"PWD"=>"Password"
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
// Call stored procedure
$sql = '{call TestProcedure(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}';
$p1 = 0;
$p2 = 0;
$p3 = 0;
$p4 = 0;
$p5 = 0;
$p6 = 0;
$p7 = 0;
$p8 = 0;
$p9 = 0;
$p10 = 0;
$params = array(
array(&$p1, SQLSRV_PARAM_INOUT),
array(&$p2, SQLSRV_PARAM_INOUT),
array(&$p3, SQLSRV_PARAM_INOUT),
array(&$p4, SQLSRV_PARAM_INOUT),
array(&$p5, SQLSRV_PARAM_INOUT),
array(&$p6, SQLSRV_PARAM_INOUT),
array(&$p7, SQLSRV_PARAM_INOUT),
array(&$p8, SQLSRV_PARAM_INOUT),
array(&$p9, SQLSRV_PARAM_INOUT),
array(&$p10, SQLSRV_PARAM_INOUT)
);
$stmt = sqlsrv_query($conn, $sql, $params);
if( $stmt === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
// Output parameters are available after consuming all resultsets.
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
// Output
echo 'Parameters values'."</br>";
echo 'Parameter 1: '.$p1."</br>";
echo 'Parameter 2: '.$p2."</br>";
echo 'Parameter 3: '.$p3."</br>";
echo 'Parameter 4: '.$p4."</br>";
echo 'Parameter 5: '.$p5."</br>";
echo 'Parameter 6: '.$p6."</br>";
echo 'Parameter 7: '.$p7."</br>";
echo 'Parameter 8: '.$p8."</br>";
echo 'Parameter 9: '.$p9."</br>";
echo 'Parameter 10: '.$p10."</br>";
?>