Using a cursor with dynamic SQL in a stored procedure

前端 未结 8 2209
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 21:37

I have a dynamic SQL statement I\'ve created in a stored procedure. I need to iterate over the results using a cursor. I\'m having a hard time figuring out the right syntax.

8条回答
  •  Happy的楠姐
    2020-11-29 22:14

    First off, avoid using a cursor if at all possible. Here are some resources for rooting it out when it seems you can't do without:

    There Must Be 15 Ways To Lose Your Cursors... part 1, Introduction

    Row-By-Row Processing Without Cursor

    That said, though, you may be stuck with one after all--I don't know enough from your question to be sure that either of those apply. If that's the case, you've got a different problem--the select statement for your cursor must be an actual SELECT statement, not an EXECUTE statement. You're stuck.

    But see the answer from cmsjr (which came in while I was writing) about using a temp table. I'd avoid global cursors even more than "plain" ones....

提交回复
热议问题