执行更新时存储过程失败

时间:2010-09-09 15:31:27

标签: php sql-server stored-procedures

我正在使用PHP的SQL Server驱动程序连接到SQL Server 2008 Express。现在,我正在尝试用存储过程替换所有SELECT,UPDATE和INSERT语句。这适用于仅包含SELECT语句的SP。但是现在我尝试用更新来做一个,并且我不断收到错误消息“直接执行SQL;没有游标。”。我可以使用相同的参数值从Management Studio调用SP。

有什么想法吗?

干杯 亚历

编辑:这是一个更新程序。有趣的是,该过程实际上执行得很好并且更新了它应该的数据。但它仍然返回错误,导致异常。

首先,失败的PHP代码:

if (! $this->Result = sqlsrv_query($this->Conn, $strQuery, $arrParameters, array("Scrollable"=>SQLSRV_CURSOR_STATIC)))
{
    $this->sendErrorMail($strQuery, $arrParameters);
    throw new Exception(4001);
}

SQL

USE [testsite]  
GO  
/****** Object:  StoredProcedure [dbo].[Items_countDownload]    Script Date: 09/09/2010 18:03:28 ******/  
SET ANSI_NULLS ON  
GO  
SET QUOTED_IDENTIFIER ON  
GO  
-- =============================================  
-- Author:      Alexis Hildebrandt  
-- Create date: 2010-09-09  
-- Description: Increases the download count by 1  
-- =============================================  
ALTER PROCEDURE [dbo].[Items_countDownload]  
    @Id INT  
AS  
BEGIN  
    -- SET NOCOUNT ON added to prevent extra result sets from  
    -- interfering with SELECT statements.  
    SET NOCOUNT ON;  

    DECLARE @DownloadCount INT = 0, @MaxCount INT = 0, @Id2 INT = 0  

    DECLARE itemCursor CURSOR SCROLL
    FOR
        SELECT Id, Downloads
        FROM Items
        WHERE Id = @Id
        OR SKU IN 
        (
            SELECT SKU FROM Items WHERE Id = @Id
        )
    FOR UPDATE OF Downloads

    OPEN itemCursor

    FETCH NEXT FROM itemCursor
    INTO @Id, @DownloadCount;

    -- Find the largest Download count across all versions of the item
    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF @MaxCount < @DownloadCount
            SET @MaxCount = @DownloadCount;
        FETCH NEXT FROM itemCursor
        INTO @Id, @DownloadCount;
    END

    -- Increase the download count by one for all versions
    FETCH FIRST FROM itemCursor
    INTO @Id, @DownloadCount;
    WHILE @@FETCH_STATUS = 0
    BEGIN
        UPDATE Items 
        SET Downloads = @MaxCount + 1
        WHERE CURRENT OF itemCursor
        FETCH NEXT FROM itemCursor
        INTO @Id, @DownloadCount;
    END

    CLOSE itemCursor;
    DEALLOCATE itemCursor;
END

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

尝试从通话中删除"Scrollable"=>SQLSRV_CURSOR_STATIC选项。您调用的过程不会向PHP代码返回任何打开的游标。

相关问题