简体   繁体   中英

Complex stored procedure not executing when called by sqlsrv_execute

I've written a rather complex stored procedure in SQL Server 2012 that I know selects "1" multiple times when executed. When I run it in SQL Management Studio, it executes and the rows of my table update correctly. However, when I use the code below to call it from PHP, it's not executing:

$Month = 2;
$Year = 2016;
$connectionInfo = array("Database"=>"Finances", "UID"=>"sa", "PWD"=>"abcd1234");
$connection = sqlsrv_connect("localhost", $connectionInfo);
if( $connection === false ) {
     die( print_r( sqlsrv_errors(), true));
}
$stmt = sqlsrv_prepare($connection, "exec UpdateBudgetToMatchTransactions @Month=?, @Year=?", array(&$Month, &$Year));
if (sqlsrv_execute($stmt) === false) {
    die( print_r( sqlsrv_errors(), true));
}

SQL Profiler says the following is happening on the server:

declare @p1 int
set @p1=NULL
exec sp_prepexec @p1 output,N'@P1 int,@P2 int',N'exec UpdateBudgetToMatchTransactions @Month=@P1, @Year=@P2',2,2016
select @p1
go

However, the rows are not affected in the way they are when I call the stored procedure directly. Further, if I take that block of SQL and execute it in it's entirety in SQL Management Studio, the rows change how I'd expect them to.

I appreciate any assistance in determining what I'm doing wrong.

My master stored procedure did not include one line that was already in the sub procedures. I have added this line in the master stored procedure as well and now this behaves very well.

Have a look at adding "SET NOCOUNT ON" in the beginning of the procedure and give it a try !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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