Using a cursor with dynamic SQL in a stored procedure

前端 未结 8 2245
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 22:07

    After recently switching from Oracle to SQL Server (employer preference), I notice cursor support in SQL Server is lagging. Cursors are not always evil, sometimes required, sometimes much faster, and sometimes cleaner than trying to tune a complex query by re-arranging or adding optimization hints. The "cursors are evil" opinion is much more prominent in the SQL Server community.

    So I guess this answer is to switch to Oracle or give MS a clue.

    • Oracle EXECUTE IMMEDIATE into a cursor
    • Loop through an implicit cursor (a for loop implicitly defines/opens/closes the cursor!)

提交回复
热议问题