无效的光标状态
我们在名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我数了三个开始,三个结束。但它是代表光标循环的第二对 - 所以我会将您的“关闭/解除分配”移动到第二个“结束”之后,而不是之前。例如:
可能需要是:(
我还将获取下一级移出,因为否则您只能在 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:
Probably needs to be:
(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.