无效的光标状态

发布于 2024-10-17 22:55:28 字数 1887 浏览 0 评论 0原文

我们在名为 OSPP 的表上有触发器,用于将特定数据保存到表中以供以后使用。 一次向表中添加多行时,我在 SAP 中收到以下错误。

无效的光标状态

我们有 SQL Server 2005 SP3(但我在 SP1 和 SP2 上的全新 2005 安装上尝试过)

一个触发器:

CREATE TRIGGER [dbo].[tr_OSPP_Insert]
   ON  [dbo].[OSPP]
   FOR INSERT
AS 
BEGIN
    Declare @ItemCode varchar(255)
    Declare @CardCode varchar(255)
    Declare @Price decimal(18,2)
    Declare @ListNum bigint 
    Declare @ID bigint
    Declare @Remote char(1)
 
 
    DECLARE db_cursor CURSOR FOR 
    SELECT ItemCode, CardCode, Price, ListNum
    FROM INSERTED
 
    OPEN db_cursor  
    FETCH NEXT
    FROM db_cursor  INTO @ItemCode, @CardCode, @Price, @ListNum
    WHILE @@FETCH_STATUS = 0
    BEGIN
 
    SELECT @Remote = isnull(U_Remote, 'N') FROM OITM WHERE ItemCode = @ItemCode
 
        IF ltrim(rtrim(upper(@Remote))) = 'Y'
        BEGIN
 
        SELECT @ID = U_ID FROM [dbo].[@BDS_MAINTENANCE]
        UPDATE [dbo].[@BDS_MAINTENANCE] set U_ID = U_ID + 1
 
        INSERT INTO [dbo].[@BDS_REMOTESPECIALPRICELIST]
        (   
            Code,
            [Name],
            U_ID,
            U_ItemCode,
            U_CardCode,
            U_Price,
            U_ListNum,
            U_TransactionType,
            U_Uploaded
        ) VALUES (
            @ID,
            '_' + cast(@ID as VARCHAR(50)),
            @ID,
            @ItemCode,
            @CardCode,
            @Price,
            @ListNum,
            1,
            0
        )
 
 
    FETCH NEXT
    FROM db_cursor INTO @ItemCode, @CardCode, @Price, @ListNum
    END
 
    CLOSE db_cursor  
    DEALLOCATE db_cursor
    
 
END
 
END

我们也尝试过:

CREATE TRIGGER [dbo].[tr_OSPP_Insert]
   ON  [dbo].[OSPP]
   FOR INSERT
AS 
BEGIN
 
    SELECT * INTO [@TEMPTABLE222] FROM INSERTED 
 
END

但仍然遇到相同的错误。

你们知道出了什么问题吗?

提前致谢!

We have triggers on a table called OSPP to save specific data to a table for later use.
I get the following error in SAP when adding more than one line at a time to the table.

Invalid Cursor State

We have SQL Server 2005 SP3 (but I tried it on a clean 2005 install, on SP1 and SP2)

The one trigger :

CREATE TRIGGER [dbo].[tr_OSPP_Insert]
   ON  [dbo].[OSPP]
   FOR INSERT
AS 
BEGIN
    Declare @ItemCode varchar(255)
    Declare @CardCode varchar(255)
    Declare @Price decimal(18,2)
    Declare @ListNum bigint 
    Declare @ID bigint
    Declare @Remote char(1)
 
 
    DECLARE db_cursor CURSOR FOR 
    SELECT ItemCode, CardCode, Price, ListNum
    FROM INSERTED
 
    OPEN db_cursor  
    FETCH NEXT
    FROM db_cursor  INTO @ItemCode, @CardCode, @Price, @ListNum
    WHILE @@FETCH_STATUS = 0
    BEGIN
 
    SELECT @Remote = isnull(U_Remote, 'N') FROM OITM WHERE ItemCode = @ItemCode
 
        IF ltrim(rtrim(upper(@Remote))) = 'Y'
        BEGIN
 
        SELECT @ID = U_ID FROM [dbo].[@BDS_MAINTENANCE]
        UPDATE [dbo].[@BDS_MAINTENANCE] set U_ID = U_ID + 1
 
        INSERT INTO [dbo].[@BDS_REMOTESPECIALPRICELIST]
        (   
            Code,
            [Name],
            U_ID,
            U_ItemCode,
            U_CardCode,
            U_Price,
            U_ListNum,
            U_TransactionType,
            U_Uploaded
        ) VALUES (
            @ID,
            '_' + cast(@ID as VARCHAR(50)),
            @ID,
            @ItemCode,
            @CardCode,
            @Price,
            @ListNum,
            1,
            0
        )
 
 
    FETCH NEXT
    FROM db_cursor INTO @ItemCode, @CardCode, @Price, @ListNum
    END
 
    CLOSE db_cursor  
    DEALLOCATE db_cursor
    
 
END
 
END

We also tried :

CREATE TRIGGER [dbo].[tr_OSPP_Insert]
   ON  [dbo].[OSPP]
   FOR INSERT
AS 
BEGIN
 
    SELECT * INTO [@TEMPTABLE222] FROM INSERTED 
 
END

But still get the same error.

Do you guys have any idea what is wrong?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2024-10-24 22:55:28

我数了三个开始,三个结束。但它是代表光标循环的第二对 - 所以我会将您的“关闭/解除分配”移动到第二个“结束”之后,而不是之前。例如:

    FETCH NEXT
    FROM db_cursor INTO @ItemCode, @CardCode, @Price, @ListNum
    END

    CLOSE db_cursor  
    DEALLOCATE db_cursor
END

可能需要是:(

    END
    FETCH NEXT
    FROM db_cursor INTO @ItemCode, @CardCode, @Price, @ListNum

END
CLOSE db_cursor  
DEALLOCATE db_cursor

我还将获取下一级移出,因为否则您只能在 IF 条件内向前移动光标)


和一个样式注释(无法抗拒)。通常认为在触发器正文中SET NOCOUNT ON 是一种很好的做法,以避免发送大量额外的n 行受影响的消息。

I count three Begins, and three Ends. But it's the second pair that represent the cursor loop - so I'd move your Close/Deallocate to be after the second End, rather than before. E.g:

    FETCH NEXT
    FROM db_cursor INTO @ItemCode, @CardCode, @Price, @ListNum
    END

    CLOSE db_cursor  
    DEALLOCATE db_cursor
END

Probably needs to be:

    END
    FETCH NEXT
    FROM db_cursor INTO @ItemCode, @CardCode, @Price, @ListNum

END
CLOSE db_cursor  
DEALLOCATE db_cursor

(I've also moved the fetch next one level out, since otherwise you only move the cursor forwards inside your IF condition)


And one style comment (can't resist). It's generally considered good practice to SET NOCOUNT ON within the body of a trigger, to avoid sending lots of extra n rows affected messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文