有没有办法在 SQL Server 游标中使用参数?
我在数据库中有父子关系。我需要做的是循环遍历父级的查询,并使用父级的主键获取其子级。我遇到的问题是我需要使用参数化游标(传入密钥)来执行此操作。
SQL Server中有这样的东西或者模仿它的技巧吗?我尝试这样做,但它不起作用:
DECLARE @value VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
SET @someKey = 12345;
OPEN main_curs
FETCH NEXT FROM main_curs INTO @value;
CLOSE main_curs
DEALLOCATE main_curs
但它似乎没有让我设置@someKey。
对此的任何帮助将不胜感激。谢谢!
更新
我应该包含更多信息,因为我使示例看起来太简单了。我有多个需要使用的 @someKey 值。如前所述,我有亲子关系,最多可以有 6 个孩子。因此,我正在获取父母列表及其各自的列,并对其进行迭代。在 WHILE-LOOP 中,我想从父级获取主键并调用另一个游标来获取子级信息(返回不同的列)。因此,我将使用不同的 @someKey 值集对子光标进行多次调用。希望这是有道理的。
I have a parent-child relationship in the database. What I need to do is loop through the parent's query, and using the parent's primary key, got get its children. The issue I am having is that I need to use a parameterized cursor (pass in the key) to do this.
Is there such a thing in SQL Server or a trick to mimic this? I tried doing this, but it didn't work:
DECLARE @value VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
SET @someKey = 12345;
OPEN main_curs
FETCH NEXT FROM main_curs INTO @value;
CLOSE main_curs
DEALLOCATE main_curs
But it seems that it doesn't pick up me setting the @someKey.
Any help on this would be greatly appreciated. Thanks!
UPDATE
I should include more information as I made the example seem too simple. I have multiple @someKey values that I need to use. As mentioned before, I have a parent-child relationship and I can have up to 6 children. So I am getting a list of parents and it's respective columns and iterating through it. While in the WHILE-LOOP, I wanted to get the primary key from the parent and call another cursor to get the child information (different columns returned). So I would do multiple calls to the child cursor with different @someKey values set. Hope that makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要的是 2 个游标 - 一个用于父游标,一个用于子游标。确保子游标是在循环内部而不是外部声明的。如果您在外部声明,它将不起作用。
例如:
What you need is 2 cursors - one for the parent and one for the child .Make sure the child cursor is DECLARED inside the LOOP not outside.it will not work if you declare outside.
eg :
以下是如何使用“EXEC()”函数声明具有动态 SQL 的游标。令人惊讶的是,这确实有效。例如:
Here is how you can declare a cursor with dynamic SQL, using the 'EXEC()' function. Surprisingly this does work. For example:
您可以尝试的一件事是使用嵌套游标。页面底部的一个示例是:使用嵌套游标生成报告输出。
One thing you could try is using nested cursors. An example of this is on the bottom of the page titled: Using nested cursors to produce report output.
在另一个地方,有人建议使用存储过程(编译的 SQL 而不是临时脚本),但这也不起作用。这是另一个 MWE,相当清楚地显示了该问题:
看来变量(及其当时的值)绑定到“声明...游标”而不是打开的游标。
In another place, someone suggested using a stored procedure (compiled SQL rather than an ad-hoc script) but that doesn't work either. Here's another MWE that shows the issue fairly clearly:
It appears that the variable (and its value at the time) gets bound to the "declare ... cursor" rather than the open cursor.
您需要在游标声明之前设置
@someKey = 12345;
例如:you need to set
@someKey = 12345;
before Cursor Declaration such as:你似乎把事情的顺序搞错了,而且你实际上并没有在光标内做任何事情?
You seem to have the order of things wrong, and you're not actually doing anything inside the cursor?
如果要使用 CTE 迭代递归层次结构,请参阅使用公用表表达式的递归查询。您可以在递归 CTE 上声明游标,例如:
但大多数情况下,递归 CTE 可以完全消除对游标的需要。
If you want to iterate over a recursive hierarchy use CTEs, see Recursive Queries Using Common Table Expressions. You can declare your cursor over the recursive CTE, eg:
But most often the recursive CTEs can completely eliminate the need for a cursor.