SQL Server存储过程游标名称冲突

发布于 2024-09-15 07:47:05 字数 138 浏览 4 评论 0原文

我遇到过这样一种情况:一个 SQL Server 存储过程正在调用另一个 SQL Server 存储过程,并且两者碰巧都使用具有相同名称的游标。这导致了冲突,因为显然游标对声明它们的存储过程没有可见性限制。

有没有办法使游标对声明它们的过程私有?

I have a situation where one SQL Server stored procedure is calling another one and both happen to use cursors that have the same name. This is causing a conflict as apparently the cursors don't have visibility restricted to the stored procedure where they are declared.

Is there any way to make the cursors private to the procedure they were declared in?

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

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

发布评论

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

评论(6

深居我梦 2024-09-22 07:47:05

声明光标时使用 LOCAL 选项。

本地

指定光标的范围
是批次本地的,已存储
过程或触发器,其中
光标已创建。光标名称是
仅在此范围内有效。这
光标可以被本地引用
批处理中的游标变量,存储
过程,或触发器,或存储的
过程输出参数。输出
参数用于传递本地
光标返回到调用批次,
存储过程或触发器,它
可以将参数分配给游标
之后引用光标的变量
存储过程终止。这
当以下情况时,游标被隐式释放:
批处理、存储过程或
触发器终止,除非光标
在 OUTPUT 中传回
范围。如果它被传回
OUTPUT 参数,光标为
当最后一个变量被释放时
引用它已被释放或消失
超出范围。

Use the LOCAL option when declaring the cursor.

LOCAL

Specifies that the scope of the cursor
is local to the batch, stored
procedure, or trigger in which the
cursor was created. The cursor name is
only valid within this scope. The
cursor can be referenced by local
cursor variables in the batch, stored
procedure, or trigger, or a stored
procedure OUTPUT parameter. An OUTPUT
parameter is used to pass the local
cursor back to the calling batch,
stored procedure, or trigger, which
can assign the parameter to a cursor
variable to reference the cursor after
the stored procedure terminates. The
cursor is implicitly deallocated when
the batch, stored procedure, or
trigger terminates, unless the cursor
was passed back in an OUTPUT
parameter. If it is passed back in an
OUTPUT parameter, the cursor is
deallocated when the last variable
referencing it is deallocated or goes
out of scope.

羁绊已千年 2024-09-22 07:47:05

将游标的范围限制为存储过程

是的,您可以使用DECLARE CURSOR LOCAL

Yes you can restrict the scope of a cursor to the stored procedure with

DECLARE CURSOR LOCAL

别在捏我脸啦 2024-09-22 07:47:05

游标是本地游标还是全局游标?...将它们设置为本地或仅更改其中一个游标的名称并完成它

Are the cursors local cursors or global cursors?...Make them local or just change the name of one of the cursors and be done with it

非要怀念 2024-09-22 07:47:05

除非您特别需要全局光标,否则请使用本地光标。

如果您的游标声明不包含关键字 LOCAL 或 GLOBAL,则 SQL Server 将使用其默认服务器配置。您可以使用以下查询确定默认配置:

SELECT is_local_cursor_default FROM sys.databases WHERE name = DB_NAME();

Unless you specifically need a global cursor, use a local cursor.

If your cursor declaration does not include the keyword LOCAL or GLOBAL, then SQL Server will use its default server configuration. You can determine the default configuration by using this query:

SELECT is_local_cursor_default FROM sys.databases WHERE name = DB_NAME();
夏至、离别 2024-09-22 07:47:05

默认情况下,如果在声明游标时未填写 LOCAL / GLOBAL 开关,它将被隐式设置为 LOCAL。不过,我确实建议定义 LOCAL ,以便其他人在阅读代码时更清楚。

请参考以下文章注释:

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-cursor-transact-sql

如果您在同一范围内编写两个游标(相同的存储程序),只需为它们命名不同的名称即可。

By default, if the LOCAL / GLOBAL switch isn't filled out when declaring the cursor, it will be implicitly set to LOCAL. I do however suggest that LOCAL be defined for other peoples clarity when reading your code.

Please refer to the following article note:

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-cursor-transact-sql

If you are writing two cursors within the same scope (same stored procedure), simply name them different names.

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