SQL Cursors…Any use cases you would defend?

后端 未结 10 646
萌比男神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:39

    Having to use a cursor is generally a sign that you are doing in the database what ought to be done in the application. As others have said, cursors are generally needed when a stored procedure is calculating running totals, or when you're generating code and/or meta-programming.

    But why are you doing that kind of work in a stored procedure in the first place? Is that really the best use of your database server? Is T-SQL really the right language to use when generating code?

    Sure, sometimes the answer is "yes," or, more likely, "no, but it's simpler this way." In my view, keeping things simple trumps premature optimization any day of the week. So I use cursors. But when I think I need to use a cursor, the universe is asking me a question that I should really have a good answer to.

提交回复
热议问题