普通网友 2016-02-16 11:13
浏览 65
已采纳

从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?

  • 写回答

1条回答 默认 最新

  • dtwy2858 2016-02-16 11:36
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法