我有一个类似以下的现有应用程序:
//create table test_table( id int identity, n int )
$connectionInfo = array( "Database"=>$Database, "UID"=>$User, "PWD"=>$Password);
$Conn = sqlsrv_connect( $Server, $connectionInfo) ;
if ( $Conn === false )
{
print "*** CONNECTION FAILED for database $Database, user $User, server $Server ***" ;
die() ;
}
sqlsrv_configure( 'WarningsReturnAsErrors', 0 ) ;
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$ExecutedCommand = sqlsrv_query( $Conn, "insert test_table output inserted.id values( 123 )", $params, $options ) ;
//$ExecutedCommand = sqlsrv_query( $Conn, "select count(*) from test_table", $params, $options ) ;
if ( $ExecutedCommand === false )
{
print "*** COMMAND FAILED ***\n" ;
print_r( sqlsrv_errors() ) ;
die() ;
}
print "rows=" . sqlsrv_num_rows( $ExecutedCommand ) . "\n" ;
print_r( sqlsrv_fetch_array( $ExecutedCommand ) ) ;
想法是插入一行,并输出新插入的标识值,以创建一些子记录。 输出真的很令人惊讶:
rows=-1
Array
(
[0] => 4
[id] => 4
)
换句话说,sqlsrv_fetch_array返回了期望的结果,但问题是sqlsrv_num_rows返回-1,就像发生错误一样。 如果我用例如SELECT语句替换INSERT..OTPUT(参见注释掉的行),一切都很好。 请注意,如果我这样做:
$ExecutedCommand = sqlsrv_query( $Conn, "insert test_table values( 123 )", $params, $options ) ;
$ExecutedCommand = sqlsrv_query( $Conn, "select n=SCOPE_IDENTITY()", $params, $options ) ;
问题仍然存在。但是,如果问题确实不,则执行以下操作:
$ExecutedCommand = sqlsrv_query( $Conn, "insert test_table values( 123 )", $params, $options ) ;
$ExecutedCommand = sqlsrv_query( $Conn, "select n=SCOPE_IDENTITY() INTO #t", $params, $options ) ;
$ExecutedCommand = sqlsrv_query( $Conn, "select n FROM #t", $params, $options ) ;
你知道发生了什么吗? 细节: PHP 7.0.9(cli)(建立时间:2016年7月20日11:08:23)(ZTS) Windows 10 SQL Server 2016 sqlsrv,来自php -i的信息:
sqlsrv support => enabled
ExtensionVer => 4.0.8629.2
Directive => Local Value => Master Value
sqlsrv.ClientBufferMaxKBSize => 10240 => 10240
sqlsrv.LogSeverity => 0 => 0
sqlsrv.LogSubsystems => 0 => 0
sqlsrv.WarningsReturnAsErrors => On => On