从php调用时,SQL Server存储过程中止
I have spent hours trying to get to the bottom of this and it is driving me la la.
I have a simple stored procedure that just inputs 10,000 rows into a table. This is done because it takes long enough to prove the point (about 12 seconds).
create table lala (id int not null identity(1,1), txt varchar(100))
go
CREATE PROCEDURE lalatest
AS
BEGIN
DECLARE @x int;
SET @x = 10000;
WHILE @x > 0
BEGIN
INSERT INTO lala (txt) VALUES ('blah ' + CAST(@x AS varchar(10)));
SET @x = @x - 1;
END;
END;
When I run the stored procedure from within SQL Server Management Studio, 10,000 rows are inserted into the table.
When I run it from php it just aborts after about 150 rows (the number of rows varies, suggesting a timeout issue) with no error messages or any indication that it has finished early
The remote query timeout
setting is set to the default of 600 seconds, so its not that.
The php:
$sql = "exec lalatest";
sqlsrv_query($cn, $sql);
I have tried specifying a timeout value (which should be indefinite by default anyway) on the sqlsrv_query line and it made no difference.
I'm using php 5.6.7
and Microsoft SQL Server 2012 (SP3) (KB3072779) - 11.0.6020.0 (X64) Oct 20 2015 15:36:27 Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor)
I have all errors and warnings turned on
Anyone got any ideas as to what I need to do to make this work?
我花了好几个小时试图找到它的底部并且它驱使我拉啦。 p>
我有一个简单的存储过程,只需在表中输入10,000行。 这样做是因为它需要足够长的时间来证明这一点(大约12秒)。 p>
create table lala(id int not null identity(1,1),txt varchar( 100))
CREATE PROCEDURE lalatest
AS
BEGIN
DECLARE @x int;
SET @x = 10000;
WHILE @x> 0
BEGIN
INSERT INTO lala(txt)VALUES('blah'+ CAST(@x AS varchar(10)));
SET @x = @x - 1;
END;
END;
code> pre>
当我从SQL Server Management Studio中运行存储过程时,会在表中插入10,000行。 p>
当我运行时 它来自php它只是在大约150行之后中止(行数变化,提示超时问题)没有错误消息或任何早期完成的迹象 p>
remote 查询超时 code>设置默认设置为600秒,所以不是这样。 p>
php: p>
$ sql =“exec lalatest”;
sqlsrv_query($ cn,$ sql);
code> pre>
我已经尝试指定超时值(默认情况下应默认为无限期) 在sqlsrv_query行上并没有什么区别。 p>
我正在使用php 5.6.7 p>
和Microsoft SQL Server 2012(SP3)(KB3072779) ) - 11.0.6020.0(X64)
2015年10月20日15:36:27
Windows NT 6上的标准版(64位) .1(Build 7601:Service Pack 1)(管理程序) p>
我打开了所有错误和警告 p>
任何人都对什么有任何想法 我需要做的才能使这个工作? p>
div>
Would you believe it, I have made it work. I added SET NOCOUNT ON
as the first line in the stored procedure and now it works from php as well.
Looks like having the rows affected info throws the php sqlsrv driver a wobbly. Strange how it only does it after a certain time (about a second), perhaps something to do with when the message buffer is flushed or something. Yes, I'm guessing, but this has solved the problem, so I'm happy.
CREATE PROCEDURE lalatest
AS
BEGIN
SET NOCOUNT ON; -- must be added so that it will run from php
DECLARE @x int;
SET @x = 10000;
WHILE @x > 0
BEGIN
INSERT INTO lala (txt) VALUES ('blah ' + CAST(@x AS varchar(10)));
SET @x = @x - 1;
END;
END;