SQL Server:游标性能

发布于 2024-12-05 20:37:05 字数 1042 浏览 0 评论 0原文

我在存储过程中使用游标,除了游标之外没有其他方法。

我已将我的脚本粘贴在这里。我想提高这个游标的性能。 一点背景。我传递“名称”和“操作”,光标正在调用不同的存储过程作为返回。我在所有这些存储过程中传递了另一个参数 (ID)。请提高性能。

ALTER PROCEDURE dbo.Driver_sp
AS

TRUNCATE TABLE Driver_tbl

INSERT INTO Driver_tbl (ID,Name,Action,vipbatchid,processseq)
SELECT ID,Name,Action,batchid,ProcessSeq
FROM pcis_tbl


DECLARE @name varchar(20),
        @action varchar(20),
        @ID bigint

DECLARE cur CURSOR LOCAL FOR
    SELECT id,name,action FROM Driver_tbl
    ORDER BY processseq

OPEN cur  

FETCH NEXT FROM cur INTO @id, @name,@action

WHILE @@FETCH_STATUS = 0

BEGIN

   if (@name = 'A' and @action = 'Update')  
   BEGIN exec A_sp @ID END 
     else 
     if (@name = 'B' and @action = 'Update') 
     BEGIN exec B_sp @ID END 
       else
       if (@name = 'C' and @action = 'Update')
       BEGIN exec C_sp @ID END 
         else 
         if (@name = 'D' and @action = 'Update') 
         BEGIN exec D_sp @ID END 


fetch next FROM cur INTO @id, @name,@action

END
CLOSE cur
DEALLOCATE cur

谢谢 !!

I am using cursor in a stored procedure and there is no other way but the cursor.

I have pasted my script here. I want to improve the performance of this cursor.
A little background. Im passing 'name' and 'action' and cursor is calling different stored procedures in return. I have passed another parameter (ID) in all of those stored procedures. Please improve the performance.

ALTER PROCEDURE dbo.Driver_sp
AS

TRUNCATE TABLE Driver_tbl

INSERT INTO Driver_tbl (ID,Name,Action,vipbatchid,processseq)
SELECT ID,Name,Action,batchid,ProcessSeq
FROM pcis_tbl


DECLARE @name varchar(20),
        @action varchar(20),
        @ID bigint

DECLARE cur CURSOR LOCAL FOR
    SELECT id,name,action FROM Driver_tbl
    ORDER BY processseq

OPEN cur  

FETCH NEXT FROM cur INTO @id, @name,@action

WHILE @@FETCH_STATUS = 0

BEGIN

   if (@name = 'A' and @action = 'Update')  
   BEGIN exec A_sp @ID END 
     else 
     if (@name = 'B' and @action = 'Update') 
     BEGIN exec B_sp @ID END 
       else
       if (@name = 'C' and @action = 'Update')
       BEGIN exec C_sp @ID END 
         else 
         if (@name = 'D' and @action = 'Update') 
         BEGIN exec D_sp @ID END 


fetch next FROM cur INTO @id, @name,@action

END
CLOSE cur
DEALLOCATE cur

Thanks !!

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

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

发布评论

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

评论(1

安静 2024-12-12 20:37:05

假设您使用的是 TSQL,请将 CURSOR 声明更改为

DECLARE cur CURSOR FAST_FORWARD FOR

Assuming, you're using TSQL, change CURSOR declaration to

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