2010-09-09 64 views
3

我正在使用PHP的SQL Server驅動程序連接到SQL Server 2008 Express。現在,我試圖用存儲過程替換所有的SELECT,UPDATE和INSERT語句。這對於只包含SELECT語句的SP來說工作正常。但是現在我試圖用update來做一個,並且不斷收到錯誤消息「直接執行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 
+1

如果您可以顯示您的更新存儲過程代碼,那麼它可能會幫助我們確定問題 – 2010-09-09 15:35:08

+0

您能用文字描述這段代碼的目的是什麼?它看起來像是通過直接的'update'語句而不是使用遊標可以更高效地實現的。 – 2010-09-10 12:01:16

+0

嗨馬丁,我知道它被炸燬了。但是它需要處理兩件事情:計算項目下載量並確保計數器對同一下載的不同版本同步。 – Alexis 2010-09-10 13:24:41

回答

0

嘗試從呼叫"Scrollable"=>SQLSRV_CURSOR_STATIC選項。您正在調用的過程不會將任何打開的遊標返回給PHP代碼。

+0

謝謝隊友,就是這樣! – Alexis 2010-09-10 13:22:37