Using cursor in OLTP databases (SQL server)

2019-07-29 23:06发布

问题:

Is it safe to use cursors in stored procs that are called from a website? There is the obvious performance hit, but what I am trying to raise here is the issue with the variable @@Fetch_status. The scope of @@Fetch_status used in stored proc, is a connection. Isn't it possible that two different users call the same stored proc, from the same connection, thru the UI? Wouldn't that cause unexpected results?

In other words, would the fact that @@Fetch_status is global not just to a scope but to the entire connection, cause any concerns?

NOTE: This post is not about whether using a cursor is a good idea. I'd appreciate responses relevant to @@Fetch_status. Please come here to discuss the pros and cons of cursors

回答1:

Unless you are using global cursors you should be safe

that said, why are you using cursors, almost everything can be accomplished set based in sql server and it will be many times faster in almost every situation