SQL Server:游标性能
我在存储过程中使用游标,除了游标之外没有其他方法。
我已将我的脚本粘贴在这里。我想提高这个游标的性能。 一点背景。我传递“名称”和“操作”,光标正在调用不同的存储过程作为返回。我在所有这些存储过程中传递了另一个参数 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您使用的是 TSQL,请将 CURSOR 声明更改为
Assuming, you're using TSQL, change CURSOR declaration to