SQL Server存储过程游标名称冲突
我遇到过这样一种情况:一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
声明光标时使用 LOCAL 选项。
Use the LOCAL option when declaring the cursor.
显然有一个本地关键字。
http://www.codeguru.com/cpp/data/ mfc_database/sqlserver/article.php/c7177
Apparently there is a local keyword.
http://www.codeguru.com/cpp/data/mfc_database/sqlserver/article.php/c7177
将游标的范围限制为存储过程
是的,您可以使用DECLARE CURSOR LOCAL
Yes you can restrict the scope of a cursor to the stored procedure with
DECLARE CURSOR LOCAL
游标是
本地
游标还是全局
游标?...将它们设置为本地
或仅更改其中一个游标的名称并完成它Are the cursors
local
cursors orglobal
cursors?...Make themlocal
or just change the name of one of the cursors and be done with it除非您特别需要全局光标,否则请使用本地光标。
如果您的游标声明不包含关键字 LOCAL 或 GLOBAL,则 SQL Server 将使用其默认服务器配置。您可以使用以下查询确定默认配置:
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:
默认情况下,如果在声明游标时未填写 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.