TSQL 更改游标定义
我正在使用光标。
DECLARE @column1 NVARCHAR(MAX);
DECLARE cursor_name CURSOR FOR
SELECT c1
FROM table_name;
OPEN cursor_name;
FETCH cursor_name INTO @column1;
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH cursor_name INTO @column1;
END
CLOSE cursor_name;
DEALLOCATE cursor_name;
现在我的问题是,使用后可以更改光标cursor_name
的定义吗?我的意思类似于:
DECLARE cursor_name CURSOR FOR
SELECT c2
FROM table_name2;
使用相同的游标名称 cursor_name
,但定义已更改。 如果这可能的话,该怎么做?
谢谢。
I'm using a cursor.
DECLARE @column1 NVARCHAR(MAX);
DECLARE cursor_name CURSOR FOR
SELECT c1
FROM table_name;
OPEN cursor_name;
FETCH cursor_name INTO @column1;
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH cursor_name INTO @column1;
END
CLOSE cursor_name;
DEALLOCATE cursor_name;
Now my question is, can I change the cursor cursor_name
's definition after using it? I mean something similar to:
DECLARE cursor_name CURSOR FOR
SELECT c2
FROM table_name2;
Using the same cursor name cursor_name
, but the defination is changed.
If this is possible, how to do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的,但它有在您解除分配之后。你试过这个但不起作用还是什么?
您可能还想看看是否真的需要光标。它们会损害性能,并且使用它们的 SQL 通常可以在没有它们的情况下重写。
看看这篇文章详细介绍如何去做。他们使用不太常见的 DECLARE/SET 语法来声明游标(至少在我的世界中不常见)。这是他们提供的示例:
Yes it's possible, but it has to be after your DEALLOCATE it. Have you tried this and it's not working or something?
You may also want to look at whether you really need a cursor. They hurt performance and SQL that uses them can often be rewritten without them.
Take a look at this article which goes over how to do it. They use the less common DECLARE/SET syntax for declaring a cursor (uncommon in my world at least). Here is the example they provide:
避免讨论游标是否必要,从纯粹的技术角度来看,一旦游标被关闭并释放,您就可以自由地再次重用该名称。
Avoiding the discussion of whether or not a cursor is necessary, from a purely technical point of view, once the cursor is closed and deallocated, you are free to reuse the name again.