SQL Cursors…Any use cases you would defend?

后端 未结 10 688
萌比男神i
萌比男神i 2021-01-06 23:01

I\'ll go first.

I\'m 100% in the set-operations camp. But what happens when the set logic on the entire desired input domain leads to a such a large retrieval that

10条回答
  •  [愿得一人]
    2021-01-06 23:49

    There are lots of different cursor behaviors.

    • STATIC vs KEYSET vs DYNAMIC
    • SCROLL vs FORWARD ONLY vs FAST FORWARD
    • INSENSITIVE or not
    • OPTIMISTIC or READ ONLY or not
    • LOCAL vs GLOBAL (at least this is easy)

    You should never use a cursor unless you can explain all of these options and which ones are on by default.

    And so, I never do.

    Instead, when I feel the urge to loop over something in T-SQL... I load it into a variable table, which is something like a LOCAL STATIC SCROLL cursor... except that it can be indexed and joined (edit: and the downside of preventing the use of parallelism).

提交回复
热议问题