简体   繁体   English

PHP sql_srv_rows_affected返回多个输出

[英]PHP sql_srv_rows_affected to return multiple outputs

I have created a stored procedure to import a list of records. 我创建了一个存储过程来导入记录列表。 This stored procedure INSERTs to a temporary table then MERGEs based on if contract numbers match. 此存储过程将插入到临时表中,然后根据合同编号是否匹配来合并。 WHEN NOT MATCHED BY TARGET it INSERTs into the real table and WHEN NOT MATCHED BY SOURCE it DELETEs from the real table. 如果未按目标匹配,则将其插入到实际表中;如果未按目标匹配,则将其从实际表中删除。

This works fine, the problem I am working with is in PHP trying to get it to echo the the SQL SERVER messages: 这工作正常,我正在使用的问题是在PHP中试图使其回显SQL SERVER消息:

(2384 row(s) affected)

(2 row(s) affected)

The first message of "(2384 row(s) affected)" shows up with sql_srv_rows_affected, but the second one "(2 row(s) affected)" does not. 第一条消息“(受影响的2384行)”显示为sql_srv_rows_affected,而第二条消息“(受影响的2行)”则没有。 Which the first one is the import into the temporary table and the second result is the amount of rows that were affected by the MERGE statement. 第一个是导入到临时表中,第二个结果是受MERGE语句影响的行数。

Here is my code to echo out rows affected: 这是我的代码来回显受影响的行:

$rows_affected = sqlsrv_rows_affected($Result);
if( $rows_affected === false) {
    die( print_r( sqlsrv_errors(), true));
} 
elseif( $rows_affected == -1) {
    echo "No information available.<br />";
} 
else {
    echo $rows_affected." rows were updated.<br />";
}

Am I able to do a while statement to achieve what I am looking for? 我能否做一阵子陈述来实现我的期望?

Thank you in advance! 先感谢您!

EDIT: Here is the code that worked: 编辑:这是工作的代码:

$rows_affected = sqlsrv_rows_affected( $PPListImportResult);
$next_result = sqlsrv_next_result($PPListImportResult);
if( $rows_affected === false) {
    die( print_r( sqlsrv_errors(), true));
} 
elseif( $rows_affected == -1) {
    echo "No information available.<br />";
} 
else {
    echo $rows_affected." rows were updated.<br />";
    echo $next_result." rows were updated.<br />";
}

Which if I had more than 2 results, I could change the $next_result variable into a while statement instead. 如果我有两个以上的结果,则可以将$ next_result变量更改为while语句。

For further readers, here is a bit more detailed answer: 对于更多读者,这里有一些更详细的答案:

Most RDMSs are able to execute batches (more than one query in one run). 大多数RDMS都可以执行批处理(一次运行不止一个查询)。 From the application's perspective, a stored procedure which contains more than one query is shown as a batch. 从应用程序的角度来看,包含多个查询的存储过程显示为批处理。

Using SQL Server, all statements (queries) has it's own result in the result set, but most of the functions are fetching the first one by default. 使用SQL Server,所有语句(查询)在结果集中都有它自己的结果,但是默认情况下,大多数功能都在提取第一个。

The application should set the subsequent results active using the library's related function. 应用程序应使用库的相关功能将后续结果设置为活动状态。 In the sqlsrv_* function family, this function is the mixed sqlsrv_next_result ( resource $stmt ) . sqlsrv_*函数族中,此函数是mixed sqlsrv_next_result ( resource $stmt )

Makes the next result of the specified statement active. 使指定语句的下一个结果处于活动状态。 Results include result sets, row counts, and output parameters. 结果包括结果集,行数和输出参数。

Returns TRUE if the next result was successfully retrieved, FALSE if an error occurred, and NULL if there are no more results to retrieve. 如果成功检索到下一个结果,则返回TRUE;如果发生错误,则返回FALSE;如果没有其他要检索的结果,则返回NULL。

Read more about this function in the PHP manual: http://php.net/manual/en/function.sqlsrv-next-result.php 在PHP手册中阅读有关此功能的更多信息: http : //php.net/manual/zh/function.sqlsrv-next-result.php

With an example (without error handling and other fancy stuffs): 举一个例子(没有错误处理和其他花哨的东西):

$stmt = sql_server_query($conn, $query, $params);
$affectedRows = array();
do {
  echo 'Rows affected: ' . sqlsrv_rows_affected($stmt)
} while (sqlsrv_next_result($stmt))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM